This commit is contained in:
insanity 2018-03-13 18:26:31 +09:00
commit 01e1133e85
8 changed files with 121 additions and 20 deletions

View File

@ -8,10 +8,10 @@ import {
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/take'; import 'rxjs/add/operator/take';
import 'rxjs/add/operator/map'; 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 * as AuthStore from 'packages/member/store/auth';
import { AuthSelector } from 'packages/member/store'; import { AuthSelector } from 'packages/member/store';
@ -19,7 +19,8 @@ import { AuthSelector } from 'packages/member/store';
@Injectable() @Injectable()
export class AuthGuard implements CanActivate, CanActivateChild { export class AuthGuard implements CanActivate, CanActivateChild {
constructor( constructor(
private store: Store<AuthStore.State> private store: Store<AuthStore.State>,
private cookieService: CookieService,
) { } ) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
@ -27,7 +28,11 @@ export class AuthGuard implements CanActivate, CanActivateChild {
.select(AuthSelector.select('signined')) .select(AuthSelector.select('signined'))
.map(signined => { .map(signined => {
if (!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; return false;
} }
@ -47,10 +52,6 @@ export class AuthGuard implements CanActivate, CanActivateChild {
return true; return true;
}) })
.catch(() => {
this.store.dispatch(new AuthStore.SigninRedirect(state.url));
return of(false);
})
.take(1); .take(1);
} }
} }

View File

@ -19,9 +19,8 @@ import { CookieService } from 'ngx-cookie-service';
import { RPCClient } from 'packages/core/rpc/client/RPCClient'; import { RPCClient } from 'packages/core/rpc/client/RPCClient';
import { import {
Signin,
SigninSuccess, SigninSuccess,
SigninFailure, SigninCookieSuccess,
ActionType, ActionType,
} from 'packages/member/store/auth'; } 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);
}
);
} }

View File

@ -35,8 +35,6 @@ export class RESTClient {
reportProgress?: boolean; reportProgress?: boolean;
withCredentials?: boolean; withCredentials?: boolean;
}): Observable<T> { }): Observable<T> {
// options.withCredentials = true;
return this._httpClient return this._httpClient
.request<T>(method, Location.joinWithSlash(this._baseURL, entry), options) .request<T>(method, Location.joinWithSlash(this._baseURL, entry), options)
.map( .map(

View File

@ -84,9 +84,7 @@ export class SigninComponent implements OnInit {
} }
signin() { signin() {
const signinValue = Object.assign({}, this.signinForm.value); const signinValue = Object.assign({}, this.signinForm.value);
signinValue.options = { signinValue.returnURL = this.returnURL;
returnURL: this.returnURL,
};
this.store.dispatch(new AuthStore.Signin(signinValue)); this.store.dispatch(new AuthStore.Signin(signinValue));
console.log('signin component signin fnc'); console.log('signin component signin fnc');

View File

@ -28,6 +28,16 @@ export class MemberService {
}); });
} }
public signin_cookie(authToken: string): Observable<DomainMember> {
const body = {
authToken: authToken,
};
return this.restClient.request<DomainMember>('post', '/account/signin_cookie', {
body: body,
});
}
public signup(member: Member): Observable<Member> { public signup(member: Member): Observable<Member> {
return this.restClient.request<Member>('post', '/account/signup', { return this.restClient.request<Member>('post', '/account/signup', {
body: member, body: member,

View File

@ -10,6 +10,11 @@ export enum ActionType {
SigninSuccess = '[member.auth] SigninSuccess', SigninSuccess = '[member.auth] SigninSuccess',
SigninFailure = '[member.auth] SigninFailure', SigninFailure = '[member.auth] SigninFailure',
SigninRedirect = '[member.auth] SigninRedirect', SigninRedirect = '[member.auth] SigninRedirect',
SigninCookie = '[member.auth] SigninCookie',
SigninCookieSuccess = '[member.auth] SigninCookieSuccess',
SigninCookieFailure = '[member.auth] SigninCookieFailure',
Signout = '[member.auth] Signout', Signout = '[member.auth] Signout',
SignoutSuccess = '[member.auth] SignoutSuccess', SignoutSuccess = '[member.auth] SignoutSuccess',
SignoutFailure = '[member.auth] SignoutFailure', SignoutFailure = '[member.auth] SignoutFailure',
@ -18,7 +23,7 @@ export enum ActionType {
export class Signin implements Action { export class Signin implements Action {
readonly type = ActionType.Signin; 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 { export class SigninSuccess implements Action {
@ -38,6 +43,24 @@ export class SigninRedirect implements Action {
constructor(public payload: string) {} 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 { export class Signout implements Action {
readonly type = ActionType.Signout; readonly type = ActionType.Signout;
} }
@ -57,6 +80,9 @@ export type Actions =
| SigninSuccess | SigninSuccess
| SigninFailure | SigninFailure
| SigninRedirect | SigninRedirect
| SigninCookie
| SigninCookieSuccess
| SigninCookieFailure
| Signout | Signout
| SignoutSuccess | SignoutSuccess
| SignoutFailure | SignoutFailure

View File

@ -26,12 +26,16 @@ import {
SigninSuccess, SigninSuccess,
SigninFailure, SigninFailure,
SigninRedirect, SigninRedirect,
SigninCookie,
SigninCookieSuccess,
SigninCookieFailure,
ActionType, ActionType,
} from './auth.action'; } from './auth.action';
@Injectable() @Injectable()
export class Effects { export class Effects {
private _options: any; private _returnURL: any;
constructor( constructor(
private actions$: Actions, private actions$: Actions,
@ -44,12 +48,12 @@ export class Effects {
.ofType(ActionType.Signin) .ofType(ActionType.Signin)
.map((action: Signin) => action.payload) .map((action: Signin) => action.payload)
.switchMap(payload => { .switchMap(payload => {
this._options = payload.options; this._returnURL = payload.returnURL;
return this.memberService.signin(payload.email, payload.password); return this.memberService.signin(payload.email, payload.password);
}) })
.map((result: any) => { .map((result: any) => {
const authToken = result['authToken']; const authToken = result['authToken'];
const domainMember = JSON.parse(result['domainMember']); const domainMember = result['domainMember'];
return new SigninSuccess({authToken: authToken, domainMember: domainMember}); return new SigninSuccess({authToken: authToken, domainMember: domainMember});
}) })
.catch((error: RESTError) => { .catch((error: RESTError) => {
@ -60,7 +64,7 @@ export class Effects {
signinSuccess$ = this.actions$ signinSuccess$ = this.actions$
.ofType(ActionType.SigninSuccess) .ofType(ActionType.SigninSuccess)
.do(() => { .do(() => {
this.router.navigateByUrl(this._options.returnURL); this.router.navigateByUrl(this._returnURL);
}); });
@Effect({ dispatch: false }) @Effect({ dispatch: false })
@ -71,4 +75,33 @@ export class Effects {
this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}}); this.router.navigate(['/auth/signin'], {queryParams: {returnURL: returnURL}});
}); });
@Effect()
signinCookie$: Observable<Action> = 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);
});
} }

View File

@ -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: { case ActionType.Signout: {
return { return {
...state, ...state,