Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit 115c554

Browse files
committed
added ngrx for auth
1 parent f0098cc commit 115c554

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed

src/providers/auth/auth.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@angular/core';
2+
import { AngularFireAuth } from 'angularfire2/auth';
3+
import { fromFirebaseAuthPromise } from "../firebase-utils";
4+
5+
@Injectable()
6+
export class AuthProvider {
7+
8+
constructor(public afAuth: AngularFireAuth) { }
9+
10+
signIn(user: {email, password}) {
11+
return fromFirebaseAuthPromise(
12+
this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password)
13+
);
14+
}
15+
16+
signUp(user: {email, password}) {
17+
return fromFirebaseAuthPromise(
18+
this.afAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
19+
);
20+
}
21+
22+
signOut() {
23+
return fromFirebaseAuthPromise(
24+
this.afAuth.auth.signOut()
25+
);
26+
}
27+
28+
}

src/store/actions/auth.actions.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Action } from '@ngrx/store';
2+
import { type } from "../util";
3+
4+
export const AuthActionTypes = {
5+
LOGIN_REQUESTED: type("[Auth] -LOGIN Requested-"),
6+
LOGIN_COMPLETED: type("[Auth] -LOGIN Completed-"),
7+
8+
LOGOUT_REQUESTED: type("[Auth] -LOGOUT Requested-"),
9+
LOGOUT_COMPLETED: type("[Auth] -LOGOUT Completed-"),
10+
11+
SIGNUP_REQUESTED: type("[Auth] -SIGNUP Requested-"),
12+
SIGNUP_COMPLETED: type("[Auth] -SIGNUP Completed-"),
13+
14+
AUTH_ERROR: type("[Auth] -Auth Error-")
15+
};
16+
17+
interface AuthUserPayload {
18+
user: any;
19+
}
20+
21+
export class LoginRequestedAction implements Action {
22+
type = AuthActionTypes.LOGIN_REQUESTED;
23+
constructor(public payload: AuthUserPayload) {}
24+
}
25+
26+
export class LoginCompletedAction implements Action {
27+
type = AuthActionTypes.LOGIN_COMPLETED;
28+
constructor(public payload: AuthUserPayload) {}
29+
}
30+
31+
export class AuthErrorAction implements Action {
32+
type = AuthActionTypes.AUTH_ERROR;
33+
constructor(public payload: any) {}
34+
}
35+
36+
export class LogoutRequestedAction implements Action {
37+
type = AuthActionTypes.LOGOUT_REQUESTED;
38+
constructor(public payload = null) {}
39+
}
40+
41+
export class LogoutCompletedAction implements Action {
42+
type = AuthActionTypes.LOGOUT_COMPLETED;
43+
constructor(public payload = null) {}
44+
}
45+
46+
export class SignUpRequestedAction implements Action {
47+
type = AuthActionTypes.SIGNUP_REQUESTED;
48+
constructor(public payload: AuthUserPayload) {}
49+
}
50+
51+
export class SignUpCompletedAction implements Action {
52+
type = AuthActionTypes.SIGNUP_COMPLETED;
53+
constructor(public payload: AuthUserPayload) {}
54+
}
55+
56+
export type AuthAction =
57+
| LoginRequestedAction
58+
| LoginCompletedAction
59+
| LogoutRequestedAction
60+
| LogoutCompletedAction
61+
| AuthErrorAction;

src/store/effects/auth.effects.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { NavPushAction } from './../actions/nav.actions';
2+
import { Action } from '@ngrx/store';
3+
import { AuthActionTypes, LoginCompletedAction, LogoutCompletedAction, SignUpCompletedAction, AuthErrorAction } from './../actions/auth.actions';
4+
import { AuthProvider } from './../../providers/auth/auth';
5+
import { Observable } from 'rxjs/Observable';
6+
import { Injectable } from '@angular/core';
7+
import { Actions, Effect } from '@ngrx/effects';
8+
import 'rxjs/add/operator/map';
9+
import 'rxjs/add/operator/mergeMap';
10+
import 'rxjs/add/operator/switchMap';
11+
import 'rxjs/add/operator/do';
12+
import 'rxjs/add/operator/catch';
13+
import 'rxjs/add/observable/of';
14+
import 'rxjs/add/observable/from';
15+
16+
17+
@Injectable()
18+
export class AuthEffects {
19+
20+
constructor(
21+
private authService: AuthProvider,
22+
private actions$: Actions
23+
) { }
24+
25+
@Effect()
26+
loginAction$: Observable<Action> = this.actions$
27+
.ofType(AuthActionTypes.LOGIN_REQUESTED)
28+
.map(this.toPayload)
29+
.switchMap(payload => this.authService.signIn(payload)
30+
.mergeMap(res => Observable.from([
31+
(new LoginCompletedAction({user: res})),
32+
(new NavPushAction({page: 'HomePage', toRootNav: true}))
33+
]))
34+
.catch(this.handleError)
35+
);
36+
37+
@Effect()
38+
logoutAction$: Observable<Action> = this.actions$
39+
.ofType(AuthActionTypes.LOGOUT_REQUESTED)
40+
.map(this.toPayload)
41+
.switchMap(payload => this.authService.signOut()
42+
.map(res => (new LogoutCompletedAction()))
43+
.catch(this.handleError)
44+
);
45+
46+
@Effect()
47+
signUpAction$: Observable<Action> = this.actions$
48+
.ofType(AuthActionTypes.SIGNUP_REQUESTED)
49+
.map(this.toPayload)
50+
.switchMap(payload => this.authService.signUp(payload.user)
51+
.map(res => (new SignUpCompletedAction({user: res})))
52+
.catch(this.handleError)
53+
);
54+
55+
private handleError(error) {
56+
return Observable.of(new AuthErrorAction({error: error}));
57+
}
58+
59+
private toPayload(action){
60+
return action.payload;
61+
}
62+
}

src/store/reducers/auth.reducer.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { AuthState } from './../state/auth.state';
2+
import { AuthAction, AuthActionTypes } from "../actions/auth.actions";
3+
4+
export const authInitialState: AuthState = {
5+
userData: null,
6+
isLoggedIn: false,
7+
error: null
8+
}
9+
10+
export function authReducer(state = authInitialState, action: AuthAction): AuthState {
11+
switch (action.type) {
12+
13+
case AuthActionTypes.LOGIN_COMPLETED: {
14+
return Object.assign({}, state, {
15+
userData: action.payload.user,
16+
isLoggedIn: action.payload.user != null,
17+
error: null
18+
});
19+
}
20+
21+
case AuthActionTypes.AUTH_ERROR: {
22+
return Object.assign({}, state, {
23+
error: action.payload.error
24+
});
25+
}
26+
27+
case AuthActionTypes.LOGOUT_COMPLETED: {
28+
return Object.assign({}, state, {
29+
userData: null,
30+
isLoggedIn: false
31+
});
32+
}
33+
34+
default: {
35+
return state;
36+
}
37+
}
38+
}

src/store/services/auth.store.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AuthState } from './../state/auth.state';
2+
import { LoginRequestedAction } from './../actions/auth.actions';
3+
import { AppState } from './../state/app.state';
4+
import { Injectable } from '@angular/core';
5+
//import { Observable } from 'rxjs/Observable';
6+
import { Store } from "@ngrx/store";
7+
8+
@Injectable()
9+
export class AuthStoreService {
10+
11+
private AUTH_STATE = 'auth';
12+
13+
constructor(private store: Store<AppState>) {}
14+
15+
dispatchLoginAction(user){
16+
this.store.dispatch(new LoginRequestedAction(user));
17+
}
18+
19+
getAuthError(){
20+
return this.storeSelect()
21+
.map((state: AuthState) => state.error);
22+
}
23+
24+
private storeSelect(){
25+
return this.store.select(this.AUTH_STATE);
26+
}
27+
}

src/store/state/auth.state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface AuthState {
2+
userData: any; //firebase user model
3+
isLoggedIn: boolean,
4+
error: any;
5+
}

0 commit comments

Comments
 (0)