Slide 1

Slide 1 text

Demystifying Token Authentication in NgRx

Slide 2

Slide 2 text

Great, but how do I log in? @samjulien

Slide 3

Slide 3 text

What goes where? @samjulien

Slide 4

Slide 4 text

Sam Julien @samjulien Technical Community Manager at Auth0 GDE & Angular Collaborator UpgradingAngularJS.com @samjulien

Slide 5

Slide 5 text

@samjulien State Actions Effects

Slide 6

Slide 6 text

State

Slide 7

Slide 7 text

What goes in the store? @samjulien

Slide 8

Slide 8 text

SHARI @samjulien Shared Impacted Retrieved Available Hydrated

Slide 9

Slide 9 text

export interface State { isAuthenticated: boolean; userProfile: UserProfile; accessToken: AccessToken; } @samjulien

Slide 10

Slide 10 text

export interface State { isAuthenticated: boolean; userProfile: UserProfile; accessToken: AccessToken; } @samjulien

Slide 11

Slide 11 text

export interface State { isAuthenticated: boolean; userProfile: UserProfile; accessToken: AccessToken; } @samjulien

Slide 12

Slide 12 text

export interface State { isAuthenticated: boolean; userProfile: UserProfile; accessToken: AccessToken; } @samjulien

Slide 13

Slide 13 text

Don’t keep access tokens in local storage! @samjulien

Slide 14

Slide 14 text

Actions

Slide 15

Slide 15 text

@samjulien

Slide 16

Slide 16 text

@samjulien

Slide 17

Slide 17 text

Start login Handle redirect Log in to provider

Slide 18

Slide 18 text

Handle redirect Success Error

Slide 19

Slide 19 text

@samjulien Good Action Hygiene(TM)

Slide 20

Slide 20 text

@samjulien Write actions first Don’t reuse actions Clarity > Brevity Good Action Hygiene(TM)

Slide 21

Slide 21 text

export const login = createAction( '[Login Page] Start Log In’ ); @samjulien

Slide 22

Slide 22 text

export const handleRedirect = createAction( '[Auth API] Handle Redirect' ); @samjulien

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Effects

Slide 29

Slide 29 text

Authentication service will be very thin.

Slide 30

Slide 30 text

Start login Handle redirect Log in to provider

Slide 31

Slide 31 text

Handle redirect Success Error

Slide 32

Slide 32 text

login$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.login), tap(() => this.authService.login()) ), { dispatch: false } ); @samjulien

Slide 33

Slide 33 text

login$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.login), tap(() => this.authService.login()) ), { dispatch: false } ); @samjulien

Slide 34

Slide 34 text

login$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.login), tap(() => this.authService.login()) ), { dispatch: false } ); @samjulien

Slide 35

Slide 35 text

login$ = createEffect( () => this.actions$.pipe( ofType(AuthActions.login), tap(() => this.authService.login()) ), { dispatch: false } ); @samjulien

Slide 36

Slide 36 text

handleRedirect$ = createEffect(() => this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien ) ); ... })

Slide 37

Slide 37 text

handleRedirect$ = createEffect(() => this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien ) ); ... })

Slide 38

Slide 38 text

handleRedirect$ = createEffect(() => this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien ) ); ... })

Slide 39

Slide 39 text

handleRedirect$ = createEffect(() => this.actions$.pipe( ofType(AuthActions.handleRedirect), exhaustMap(() => { @samjulien ) ); ... })

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

@samjulien State Actions Effects

Slide 53

Slide 53 text

samj.im/angular-denver @samjulien

Slide 54

Slide 54 text

samj.im/angular-denver Thank you! @samjulien