Skip to content

Commit

Permalink
Move auth service calls into constructor to simplify and centralize i…
Browse files Browse the repository at this point in the history
…ntegration steps for auth functionality
  • Loading branch information
kmaida committed Nov 19, 2019
1 parent 0d3bbe9 commit 88e1671
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
17 changes: 3 additions & 14 deletions 01-Login/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from './auth/auth.service';
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

constructor(private auth: AuthService) {}

ngOnInit() {
// On initial load, check authentication state with authorization server
// Set up local auth streams if user is already authenticated
this.auth.localAuthSetup();
// Handle redirect from Auth0 login
this.auth.handleAuthCallback();
}

export class AppComponent {
constructor() {}
}
12 changes: 9 additions & 3 deletions 01-Login/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export class AuthService {
// Create a local property for login status
loggedIn: boolean = null;

constructor(private router: Router) { }
constructor(private router: Router) {
// On initial load, check authentication state with authorization server
// Set up local auth streams if user is already authenticated
this.localAuthSetup();
// Handle redirect from Auth0 login
this.handleAuthCallback();
}

// When calling, options can be passed if desired
// https://auth0.github.io/auth0-spa-js/classes/auth0client.html#getuser
Expand All @@ -49,7 +55,7 @@ export class AuthService {
);
}

localAuthSetup() {
private localAuthSetup() {
// This should only be called on app initialization
// Set up local authentication streams
const checkAuth$ = this.isAuthenticated$.pipe(
Expand Down Expand Up @@ -79,7 +85,7 @@ export class AuthService {
});
}

handleAuthCallback() {
private handleAuthCallback() {
// Call when app reloads after user logs in with Auth0
const params = window.location.search;
if (params.includes('code=') && params.includes('state=')) {
Expand Down
17 changes: 3 additions & 14 deletions 02-Calling-an-API/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from './auth/auth.service';
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

constructor(private auth: AuthService) {}

ngOnInit() {
// On initial load, check authentication state with authorization server
// Set up local auth streams if user is already authenticated
this.auth.localAuthSetup();
// Handle redirect from Auth0 login
this.auth.handleAuthCallback();
}

export class AppComponent {
constructor() {}
}
12 changes: 9 additions & 3 deletions 02-Calling-an-API/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export class AuthService {
// Create a local property for login status
loggedIn: boolean = null;

constructor(private router: Router) { }
constructor(private router: Router) {
// On initial load, check authentication state with authorization server
// Set up local auth streams if user is already authenticated
this.localAuthSetup();
// Handle redirect from Auth0 login
this.handleAuthCallback();
}

// When calling, options can be passed if desired
// https://auth0.github.io/auth0-spa-js/classes/auth0client.html#getuser
Expand All @@ -58,7 +64,7 @@ export class AuthService {
);
}

localAuthSetup() {
private localAuthSetup() {
// This should only be called on app initialization
// Set up local authentication streams
const checkAuth$ = this.isAuthenticated$.pipe(
Expand Down Expand Up @@ -88,7 +94,7 @@ export class AuthService {
});
}

handleAuthCallback() {
private handleAuthCallback() {
// Call when app reloads after user logs in with Auth0
const params = window.location.search;
if (params.includes('code=') && params.includes('state=')) {
Expand Down

0 comments on commit 88e1671

Please sign in to comment.