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 { 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<AuthStore.State>
private store: Store<AuthStore.State>,
private cookieService: CookieService,
) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
@ -27,7 +28,11 @@ export class AuthGuard implements CanActivate, CanActivateChild {
.select(AuthSelector.select('signined'))
.map(signined => {
if (!signined) {
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);
}
}

View File

@ -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);
}
);
}

View File

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

View File

@ -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');

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> {
return this.restClient.request<Member>('post', '/account/signup', {
body: member,

View File

@ -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

View File

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