Skip to content

Commit

Permalink
feat: auth0
Browse files Browse the repository at this point in the history
  • Loading branch information
leoriofrio committed Aug 15, 2020
1 parent 24b249f commit 9ffae06
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 11,356 deletions.
54 changes: 54 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@angular/platform-browser": "~9.0.7",
"@angular/platform-browser-dynamic": "~9.0.7",
"@angular/router": "~9.0.7",
"@auth0/auth0-spa-js": "^1.11.0",
"@fortawesome/free-solid-svg-icons": "^5.13.0",
"@handsontable/angular": "^5.1.1",
"@ng-select/ng-select": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/app/app.keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export enum AppStatusForm {
}

export namespace ApiKeys {
export const API_URL = "http://104.210.146.134:3000/";
//export const API_URL = "http://localhost:3000/";
//export const API_URL = "http://104.210.146.134:3000/";
export const API_URL = "http://localhost:3000/";
}

export namespace TitleNames {
Expand Down
16 changes: 16 additions & 0 deletions src/app/shared/service/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
126 changes: 126 additions & 0 deletions src/app/shared/service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { Injectable } from '@angular/core';
import createAuth0Client from '@auth0/auth0-spa-js';
import Auth0Client from '@auth0/auth0-spa-js/dist/typings/Auth0Client';
import { from, of, Observable, BehaviorSubject, combineLatest, throwError } from 'rxjs';
import { tap, catchError, concatMap, shareReplay } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable({
providedIn: 'root'
})
export class AuthService {
// Create an observable of Auth0 instance of client
auth0Client$ = (from(
createAuth0Client({
domain: "YOUR_DOMAIN",
client_id: "YOUR_CLIENT_ID",
redirect_uri: `${window.location.origin}`
})
) as Observable<Auth0Client>).pipe(
shareReplay(1), // Every subscription receives the same shared value
catchError(err => throwError(err))
);
// Define observables for SDK methods that return promises by default
// For each Auth0 SDK method, first ensure the client instance is ready
// concatMap: Using the client instance, call SDK method; SDK returns a promise
// from: Convert that resulting promise into an observable
isAuthenticated$ = this.auth0Client$.pipe(
concatMap((client: Auth0Client) => from(client.isAuthenticated())),
tap(res => this.loggedIn = res)
);
handleRedirectCallback$ = this.auth0Client$.pipe(
concatMap((client: Auth0Client) => from(client.handleRedirectCallback()))
);
// Create subject and public observable of user profile data
private userProfileSubject$ = new BehaviorSubject<any>(null);
userProfile$ = this.userProfileSubject$.asObservable();
// Create a local property for login status
loggedIn: boolean = null;

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
getUser$(options?): Observable<any> {
return this.auth0Client$.pipe(
concatMap((client: Auth0Client) => from(client.getUser(options))),
tap(user => this.userProfileSubject$.next(user))
);
}

private localAuthSetup() {
// This should only be called on app initialization
// Set up local authentication streams
const checkAuth$ = this.isAuthenticated$.pipe(
concatMap((loggedIn: boolean) => {
if (loggedIn) {
// If authenticated, get user and set in app
// NOTE: you could pass options here if needed
return this.getUser$();
}
// If not authenticated, return stream that emits 'false'
return of(loggedIn);
})
);
checkAuth$.subscribe();
}

login(redirectPath: string = '/') {
// A desired redirect path can be passed to login method
// (e.g., from a route guard)
// Ensure Auth0 client instance exists
this.auth0Client$.subscribe((client: Auth0Client) => {
// Call method to log in
client.loginWithRedirect({
redirect_uri: `${window.location.origin}`,
appState: { target: redirectPath }
});
});
}

private handleAuthCallback() {
// Call when app reloads after user logs in with Auth0
const params = window.location.search;
if (params.includes('code=') && params.includes('state=')) {
let targetRoute: string; // Path to redirect to after login processsed
const authComplete$ = this.handleRedirectCallback$.pipe(
// Have client, now call method to handle auth callback redirect
tap(cbRes => {
// Get and set target redirect route from callback results
targetRoute = cbRes.appState && cbRes.appState.target ? cbRes.appState.target : '/';
}),
concatMap(() => {
// Redirect callback complete; get user and login status
return combineLatest([
this.getUser$(),
this.isAuthenticated$
]);
})
);
// Subscribe to authentication completion observable
// Response will be an array of user and login status
authComplete$.subscribe(([user, loggedIn]) => {
// Redirect to target route after callback processing
this.router.navigate([targetRoute]);
});
}
}

logout() {
// Ensure Auth0 client instance exists
this.auth0Client$.subscribe((client: Auth0Client) => {
// Call method to log out
client.logout({
client_id: "YOUR_CLIENT_ID",
returnTo: `${window.location.origin}`
});
});
}

}
6 changes: 3 additions & 3 deletions src/app/shared/service/export-excel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ExcelExportService {
}
}

public generateExcelFromJson(fileName: string, headers: any, data: any) {
public generateExcelFromJson(fileName: string, headers: any, data: any) {
// Create workbook and worksheet by the params from the Function
let workbook = new Workbook();
workbook = this.generateWorkBookFromJson(fileName, headers, data);
Expand Down Expand Up @@ -120,11 +120,11 @@ export class ExcelExportService {
};
cell.border = {top: {style: 'thin'}, left: {style: 'thin'}, bottom: {style: 'thin'}, right: {style: 'thin'}};
});

/*
worksheet.columns.forEach(column => {
column.width = column.header.length < 12 ? 12 : column.header.length
})

*/
// Add Detail Row Value from Json File
for (const item of Object.keys(data)) {
dataDetail = [];
Expand Down
Loading

0 comments on commit 9ffae06

Please sign in to comment.