From dfd124f62c0f117ada555648af67e394688e47fa Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 13 Mar 2018 18:22:55 +0900 Subject: [PATCH 1/2] ing --- .../store/signin-init/signin-init.effect.ts | 16 +++++++- src/packages/core/rest/client/RESTClient.ts | 2 - .../component/signin/signin.component.ts | 4 +- src/packages/member/service/member.service.ts | 10 +++++ src/packages/member/store/auth/auth.action.ts | 28 ++++++++++++- src/packages/member/store/auth/auth.effect.ts | 41 +++++++++++++++++-- .../member/store/auth/auth.reducer.ts | 23 +++++++++++ 7 files changed, 112 insertions(+), 12 deletions(-) diff --git a/src/app/commons/store/signin-init/signin-init.effect.ts b/src/app/commons/store/signin-init/signin-init.effect.ts index 1dd8bef..5f944b1 100644 --- a/src/app/commons/store/signin-init/signin-init.effect.ts +++ b/src/app/commons/store/signin-init/signin-init.effect.ts @@ -19,9 +19,8 @@ import { CookieService } from 'ngx-cookie-service'; import { RPCClient } from 'packages/core/rpc/client/RPCClient'; import { - Signin, SigninSuccess, - SigninFailure, + SigninCookieSuccess, ActionType, } from 'packages/member/store/auth'; @@ -53,4 +52,17 @@ export class Effects { } ); + @Effect({ dispatch: false }) + signinCookieSuccess$ = this.actions$ + .ofType(ActionType.SigninCookieSuccess) + .map((action: SigninCookieSuccess) => action.payload) + .do( + (result) => { + const authToken = this.cookieService.get('authToken'); + // console.log(`authToken: ${authToken}`); + const queryString = `authToken=${authToken}`; + + this.rpcClient.connect(queryString); + } + ); } diff --git a/src/packages/core/rest/client/RESTClient.ts b/src/packages/core/rest/client/RESTClient.ts index 0d986da..9ff174b 100644 --- a/src/packages/core/rest/client/RESTClient.ts +++ b/src/packages/core/rest/client/RESTClient.ts @@ -35,8 +35,6 @@ export class RESTClient { reportProgress?: boolean; withCredentials?: boolean; }): Observable { - // options.withCredentials = true; - return this._httpClient .request(method, Location.joinWithSlash(this._baseURL, entry), options) .map( diff --git a/src/packages/member/component/signin/signin.component.ts b/src/packages/member/component/signin/signin.component.ts index 23f786c..4f15d55 100644 --- a/src/packages/member/component/signin/signin.component.ts +++ b/src/packages/member/component/signin/signin.component.ts @@ -84,9 +84,7 @@ export class SigninComponent implements OnInit { } signin() { const signinValue = Object.assign({}, this.signinForm.value); - signinValue.options = { - returnURL: this.returnURL, - }; + signinValue.returnURL = this.returnURL; this.store.dispatch(new AuthStore.Signin(signinValue)); console.log('signin component signin fnc'); diff --git a/src/packages/member/service/member.service.ts b/src/packages/member/service/member.service.ts index 561b263..ef3d746 100644 --- a/src/packages/member/service/member.service.ts +++ b/src/packages/member/service/member.service.ts @@ -28,6 +28,16 @@ export class MemberService { }); } + public signin_cookie(authToken: string): Observable { + const body = { + authToken: authToken, + }; + + return this.restClient.request('post', '/account/signin_cookie', { + body: body, + }); + } + public signup(member: Member): Observable { return this.restClient.request('post', '/account/signup', { body: member, diff --git a/src/packages/member/store/auth/auth.action.ts b/src/packages/member/store/auth/auth.action.ts index b9d2e69..b752a0d 100644 --- a/src/packages/member/store/auth/auth.action.ts +++ b/src/packages/member/store/auth/auth.action.ts @@ -10,6 +10,11 @@ export enum ActionType { SigninSuccess = '[member.auth] SigninSuccess', SigninFailure = '[member.auth] SigninFailure', SigninRedirect = '[member.auth] SigninRedirect', + + SigninCookie = '[member.auth] SigninCookie', + SigninCookieSuccess = '[member.auth] SigninCookieSuccess', + SigninCookieFailure = '[member.auth] SigninCookieFailure', + Signout = '[member.auth] Signout', SignoutSuccess = '[member.auth] SignoutSuccess', SignoutFailure = '[member.auth] SignoutFailure', @@ -18,7 +23,7 @@ export enum ActionType { export class Signin implements Action { readonly type = ActionType.Signin; - constructor(public payload: {email: string, password: string, options: any}) {} + constructor(public payload: {email: string, password: string, returnURL: string}) {} } export class SigninSuccess implements Action { @@ -38,6 +43,24 @@ export class SigninRedirect implements Action { constructor(public payload: string) {} } +export class SigninCookie implements Action { + readonly type = ActionType.SigninCookie; + + constructor(public payload: {authToken: string, returnURL: string}) {} +} + +export class SigninCookieSuccess implements Action { + readonly type = ActionType.SigninCookieSuccess; + + constructor(public payload: DomainMember) {} +} + +export class SigninCookieFailure implements Action { + readonly type = ActionType.SigninCookieFailure; + + constructor(public payload: RESTError) {} +} + export class Signout implements Action { readonly type = ActionType.Signout; } @@ -57,6 +80,9 @@ export type Actions = | SigninSuccess | SigninFailure | SigninRedirect + | SigninCookie + | SigninCookieSuccess + | SigninCookieFailure | Signout | SignoutSuccess | SignoutFailure diff --git a/src/packages/member/store/auth/auth.effect.ts b/src/packages/member/store/auth/auth.effect.ts index cfee9ed..5da8206 100644 --- a/src/packages/member/store/auth/auth.effect.ts +++ b/src/packages/member/store/auth/auth.effect.ts @@ -26,12 +26,16 @@ import { SigninSuccess, SigninFailure, SigninRedirect, + SigninCookie, + SigninCookieSuccess, + SigninCookieFailure, + ActionType, } from './auth.action'; @Injectable() export class Effects { - private _options: any; + private _returnURL: any; constructor( private actions$: Actions, @@ -44,12 +48,12 @@ export class Effects { .ofType(ActionType.Signin) .map((action: Signin) => action.payload) .switchMap(payload => { - this._options = payload.options; + this._returnURL = payload.returnURL; return this.memberService.signin(payload.email, payload.password); }) .map((result: any) => { const authToken = result['authToken']; - const domainMember = JSON.parse(result['domainMember']); + const domainMember = result['domainMember']; return new SigninSuccess({authToken: authToken, domainMember: domainMember}); }) .catch((error: RESTError) => { @@ -60,7 +64,7 @@ export class Effects { signinSuccess$ = this.actions$ .ofType(ActionType.SigninSuccess) .do(() => { - this.router.navigateByUrl(this._options.returnURL); + this.router.navigateByUrl(this._returnURL); }); @Effect({ dispatch: false }) @@ -71,4 +75,33 @@ export class Effects { this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}}); }); + + @Effect() + signinCookie$: Observable = this.actions$ + .ofType(ActionType.SigninCookie) + .map((action: SigninCookie) => action.payload) + .switchMap((payload) => { + this._returnURL = payload.returnURL; + return this.memberService.signin_cookie(payload.authToken); + }) + .map((domainMember: DomainMember) => { + return new SigninCookieSuccess(domainMember); + }) + .catch((error: RESTError) => { + return of(new SigninFailure(error)); + }); + + @Effect({ dispatch: false }) + signinCookieSuccess$ = this.actions$ + .ofType(ActionType.SigninCookieSuccess) + .do(() => { + this.router.navigateByUrl(this._returnURL); + }); + + @Effect({ dispatch: false }) + signinCookieFailure$ = this.actions$ + .ofType(ActionType.SigninCookieFailure) + .map(() => { + return new SigninRedirect(this._returnURL); + }); } diff --git a/src/packages/member/store/auth/auth.reducer.ts b/src/packages/member/store/auth/auth.reducer.ts index e551cde..4d178f4 100644 --- a/src/packages/member/store/auth/auth.reducer.ts +++ b/src/packages/member/store/auth/auth.reducer.ts @@ -45,6 +45,29 @@ export function reducer(state = initialState, action: Actions): State { }; } + case ActionType.SigninCookieSuccess: { + return { + ...state, + signined: true, + error: null, + pending: false, + member: action.payload.member, + domain: action.payload.domain, + }; + } + + case ActionType.SigninCookieFailure: { + return { + ...state, + signined: false, + error: action.payload, + pending: false, + member: null, + domain: null, + }; + } + + case ActionType.Signout: { return { ...state, From 7bf20874849c394f809030dc437eee5d91810de6 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 13 Mar 2018 18:23:14 +0900 Subject: [PATCH 2/2] ing --- src/app/commons/guard/auth.guard.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/app/commons/guard/auth.guard.ts b/src/app/commons/guard/auth.guard.ts index 23e4d3a..fd98d82 100644 --- a/src/app/commons/guard/auth.guard.ts +++ b/src/app/commons/guard/auth.guard.ts @@ -8,10 +8,10 @@ import { import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable'; -import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/take'; import 'rxjs/add/operator/map'; -import { of } from 'rxjs/observable/of'; + +import { CookieService } from 'ngx-cookie-service'; import * as AuthStore from 'packages/member/store/auth'; import { AuthSelector } from 'packages/member/store'; @@ -19,7 +19,8 @@ import { AuthSelector } from 'packages/member/store'; @Injectable() export class AuthGuard implements CanActivate, CanActivateChild { constructor( - private store: Store + private store: Store, + private cookieService: CookieService, ) { } canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { @@ -27,7 +28,11 @@ export class AuthGuard implements CanActivate, CanActivateChild { .select(AuthSelector.select('signined')) .map(signined => { if (!signined) { - this.store.dispatch(new AuthStore.SigninRedirect(state.url)); + if (this.cookieService.check('authToken')) { + this.store.dispatch(new AuthStore.SigninCookie({authToken: this.cookieService.get('authToken'), returnURL: state.url})); + } else { + this.store.dispatch(new AuthStore.SigninRedirect(state.url)); + } return false; } @@ -47,10 +52,6 @@ export class AuthGuard implements CanActivate, CanActivateChild { return true; }) - .catch(() => { - this.store.dispatch(new AuthStore.SigninRedirect(state.url)); - return of(false); - }) .take(1); } }