mean stack - Authentication in angular 2 universal, nodejs -
i downloaded universal-starter nodejs , started migrating website old angular-rc4. when have implement authentication (in case it's jwt stored in localstorage), server dont have localstorage , cookie angular rendered on client.
i've follow guide: https://github.com/angular/universal-starter/issues/148 didnt work.
below code:
authentication.services.ts
import { opaquetoken } '@angular/core'; export let auth_services = new opaquetoken('auth.services'); export interface authenticationservice { forgotpassword(email: any); isauthenticated(); getcurrentuser(); refreshtoken(); signin(user : any); signout(); signup(user : any); }
server.authentication.ts
import { injectable } '@angular/core'; import { authenticationservice } './authentication.services'; @injectable() export class serverauthenticationservice implements authenticationservice { forgotpassword(email: any) { throw new error('forgot password cannot called while doing server side rendering'); } isauthenticated() { return false; } getcurrentuser(){ if(this.isauthenticated()) { return {}; } return {}; } refreshtoken() { } signin(user : any) { throw new error('login cannot called while doing server side rendering'); } signout() { throw new error('logout cannot called while doing server side rendering'); } signup(user : any) { throw new error('sign cannot called while doing server side rendering'); } }
clientauthentication.services.ts
@injectable() export class userservice implements authenticationservice { forgotpassword(email: any){ // client implementation } isauthenticated() { // client implementation } getcurrentuser() { // client implementation } refreshtoken() { // client implementation } signin(user : any){ // client implementation } signout(){ // client implementation } signup(user : any) { // client implementation } }
app.browser.module.ts
@ngmodule({ bootstrap: [ appcomponent ], declarations: [ appcomponent ], imports: [ universalmodule, // browsermodule, httpmodule, , jsonpmodule included formsmodule, sharedmodule, homemodule, aboutmodule, navbarmodule, approutingmodule ], providers: [ { provide: 'isbrowser', usevalue: isbrowser }, { provide: 'isnode', usevalue: isnode }, { provide: 'lru', usefactory: getlru, deps: [] }, { provide: auth_services, usefactory: userservice}, cacheservice ] })
app.node.module.ts
@ngmodule({ bootstrap: [ appcomponent ], declarations: [ appcomponent ], imports: [ universalmodule, // nodemodule, nodehttpmodule, , nodejsonpmodule included formsmodule, sharedmodule, homemodule, aboutmodule, navbarmodule, approutingmodule ], providers: [ { provide: 'isbrowser', usevalue: isbrowser }, { provide: 'isnode', usevalue: isnode }, { provide: 'lru', usefactory: getlru, deps: [ [new inject('lru'), new optional(), new skipself()] ] }, { provide: auth_services, usefactory: serverauthenticationservice }, cacheservice ] })
then how have same page output while navigating page on client via router transition vs on server via browser refresh?
thanks in advance
in node can use request , response properties in express application , inject them in angular application based on server on browser module. request.cookies holds objects maps kvps cookies.
for (outdated) examples on please here , here: https://github.com/angular/universal-starter/blob/master/src/node.module.ts#l43 https://github.com/angular/universal-starter/blob/master/src/browser.module.ts#l52
the code might outdated soon, principle holds. use dependency injection inject request on server in app , stub request ( might still expose browser cookies ) in version browser.
Comments
Post a Comment