Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying Token Authentication in NgRx (Angu...

Sam Julien
August 02, 2019

Demystifying Token Authentication in NgRx (Angular Denver 2019)

So, you’ve got a shiny new Angular application and you’re thrilled to be managing your state with NgRx. You’ve got your store, reducers, and actions set up, but your boss asks you an innocent question during your first demo: “How do I log in?” You stare blankly, realizing that you’ve never thought about that. Isn’t it the same as in a regular Angular application? How does authentication in NgRx work, anyway? (And not just tutorial authentication — real authentication using tokens!)

I’ve got your back in this talk. You’ll learn not only the HOW of token-based authentication in NgRx, but also the WHY. We’ll talk about managing app-wide authentication state through the store, dispatching actions through effects, and keeping your application safe using authentication best practices.

Sam Julien

August 02, 2019
Tweet

More Decks by Sam Julien

Other Decks in Technology

Transcript

  1. Sam Julien @samjulien Technical Community Manager at Auth0 GDE &

    Angular Collaborator UpgradingAngularJS.com @samjulien
  2. export const loginSuccess = createAction( '[Auth API] Login Success’, props<{

    redirectUrl: string }>() ); export const loginFailure = createAction( '[Auth API] Login Failure', props<{ error: Error }>() ); @samjulien
  3. export const loginSuccess = createAction( '[Auth API] Login Success’, props<{

    redirectUrl: string }>() ); export const loginFailure = createAction( '[Auth API] Login Failure', props<{ error: Error }>() ); @samjulien
  4. export const loginSuccess = createAction( '[Auth API] Login Success’, props<{

    redirectUrl: string }>() ); export const loginFailure = createAction( '[Auth API] Login Failure', props<{ error: Error }>() ); @samjulien
  5. export const loginSuccess = createAction( '[Auth API] Login Success’, props<{

    redirectUrl: string }>() ); export const loginFailure = createAction( '[Auth API] Login Failure', props<{ error: Error }>() ); @samjulien
  6. export const loginSuccess = createAction( '[Auth API] Login Success’, props<{

    redirectUrl: string }>() ); export const loginFailure = createAction( '[Auth API] Login Failure', props<{ error: Error }>() ); @samjulien
  7. this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien }) // handle redirect

    and process tokens this.authService.handleRedirect.pipe( map(({ redirectUrl }) => AuthActions.loginSuccess({ redirectUrl })), catchError(({ error }) => AuthActions.loginFailure({ error })) );
  8. this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien }) // handle redirect

    and process tokens this.authService.handleRedirect.pipe( map(({ redirectUrl }) => AuthActions.loginSuccess({ redirectUrl })), catchError(({ error }) => AuthActions.loginFailure({ error })) );
  9. this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien }) // handle redirect

    and process tokens this.authService.handleRedirect.pipe( map(({ redirectUrl }) => AuthActions.loginSuccess({ redirectUrl })), catchError(({ error }) => AuthActions.loginFailure({ error })) );
  10. this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien }) // handle redirect

    and process tokens this.authService.handleRedirect.pipe( map(({ redirectUrl }) => AuthActions.loginSuccess({ redirectUrl })), catchError(({ error }) => AuthActions.loginFailure({ error })) );
  11. loginSuccess$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginSucess), tap(({ redirectUrl })

    => this.router.navigate([redirectUrl])) ), { dispatch: false } ); @samjulien
  12. loginSuccess$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginSucess), tap(({ redirectUrl })

    => this.router.navigate([redirectUrl])) ), { dispatch: false } ); @samjulien
  13. loginSuccess$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginSucess), tap(({ redirectUrl })

    => this.router.navigate([redirectUrl])) ), { dispatch: false } ); @samjulien
  14. loginSuccess$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginSucess), tap(({ redirectUrl })

    => this.router.navigate([redirectUrl])) ), { dispatch: false } ); @samjulien
  15. loginFailure$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginFailure), tap(({ error })

    => { this.loggingService.error(error); this.router.navigate(['/']); }) ), { dispatch: false } ); @samjulien
  16. loginFailure$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginFailure), tap(({ error })

    => { this.loggingService.error(error); this.router.navigate(['/']); }) ), { dispatch: false } ); @samjulien
  17. loginFailure$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginFailure), tap(({ error })

    => { this.loggingService.error(error); this.router.navigate(['/']); }) ), { dispatch: false } ); @samjulien
  18. loginFailure$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.loginFailure), tap(({ error })

    => { this.loggingService.error(error); this.this.router.navigate(['/']); }) ), { dispatch: false } ); @samjulien