Skip to content

Instantly share code, notes, and snippets.

@leon
Created April 1, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leon/d3a63046ae125fe32f3789b53a8ccfb7 to your computer and use it in GitHub Desktop.
Save leon/d3a63046ae125fe32f3789b53a8ccfb7 to your computer and use it in GitHub Desktop.
NGXS auth state
import { Router } from '@angular/router';
import { Action, Selector, State, StateContext, Store } from '@ngxs/store';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase';
import { take, tap } from 'rxjs/operators';
import {
CheckSession,
LoginWithFacebook,
LoginWithGoogle,
LoginFailed,
LoginRedirect,
LoginSuccess,
LoginWithEmailAndPassword,
Logout,
LogoutSuccess,
} from './auth.actions';
import { AuthStateModel, User } from './auth.model';
@State<AuthStateModel>({
name: 'auth',
defaults: {
user: null
}
})
export class AuthState {
constructor(private store: Store, private auth: AngularFireAuth, private router: Router) {
console.log('SecurityStateModel');
// init auth by checking session
store.dispatch(new CheckSession()); // DOES NOT WORK
}
/**
* Selectors
*/
@Selector()
static getUser(state: AuthStateModel) {
return state.user;
}
/**
* Commands
*/
@Action(CheckSession)
checkSession(sc: StateContext<AuthStateModel>) {
console.log('CheckSession');
this.auth.authState.pipe(
take(1),
tap((user: User) => {
if (user) {
sc.dispatch(new LoginSuccess(user));
}
})
);
}
@Action(LoginWithGoogle)
loginWithGoogle(sc: StateContext<AuthStateModel>) {
const provider = new firebase.auth.GoogleAuthProvider();
this.auth.auth.signInWithPopup(provider).then(
(response: { user: User }) => {
sc.dispatch(new LoginSuccess(response.user));
})
.catch(error => {
sc.dispatch(new LoginFailed(error));
});
}
@Action(LoginWithFacebook)
loginWithFacebook(sc: StateContext<AuthStateModel>) {
const provider = new firebase.auth.FacebookAuthProvider();
this.auth.auth.signInWithPopup(provider).then(
(response: { user: User }) => {
sc.dispatch(new LoginSuccess(response.user));
})
.catch(error => {
sc.dispatch(new LoginFailed(error));
});
}
@Action(LoginWithEmailAndPassword)
loginWithEmailAndPassword(sc: StateContext<AuthStateModel>, action: LoginWithEmailAndPassword) {
this.auth.auth.signInWithEmailAndPassword(action.email, action.password).then(
(user: User) => {
sc.dispatch(new LoginSuccess(user));
})
.catch(error => {
sc.dispatch(new LoginFailed(error));
});
}
@Action(Logout)
logout(sc: StateContext<AuthStateModel>) {
this.auth.auth.signOut().then(
() => {
sc.dispatch(new LogoutSuccess());
});
}
/**
* Events
*/
@Action(LoginSuccess)
onLoginSuccess() {
console.log('onLoginSuccess');
this.router.navigateByUrl('/dashboard');
}
@Action(LoginRedirect)
onLoginRedirect() {
console.log('onLoginRedirect');
this.router.navigateByUrl('/auth/login');
}
@Action(LoginSuccess)
setUserStateOnSuccess(sc: StateContext<AuthStateModel>, event: LoginSuccess) {
console.log('setUserStateOnSuccess');
sc.setState({
user: event.user
});
}
@Action([LoginFailed, LogoutSuccess])
setUserStateOnFailure(sc: StateContext<AuthStateModel>) {
sc.setState({
user: undefined
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment