Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { Injectable } from '@angular/core';
import createAuth0Client from '@auth0/auth0-spa-js';
import Auth0Client from '@auth0/auth0-spa-js/dist/typings/Auth0Client';
import * as config from '../../../auth_config.json';
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: config.domain,
client_id: config.clientId,
redirect_uri: `${window.location.origin}`,
audience: config.audience
})
) as Observable).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)
import { Injectable } from '@angular/core';
import createAuth0Client from '@auth0/auth0-spa-js';
import Auth0Client from '@auth0/auth0-spa-js/dist/typings/Auth0Client';
import * as config from '../../../auth_config.json';
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: config.domain,
client_id: config.clientId,
redirect_uri: `${window.location.origin}`
})
) as Observable).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)
);
onMount(async () => {
auth0 = await createAuth0Client(config);
// Not all browsers support this, please program defensively!
const params = new URLSearchParams(window.location.search);
// Check if something went wrong during login redirect
// and extract the error message
if (params.has("error")) {
authError.set(new Error(params.get("error_description")));
}
// if code then login success
if (params.has("code")) {
// Let the Auth0 SDK do it's stuff - save some state, etc.
await auth0.handleRedirectCallback();
// Can be smart here and redirect to original path instead of root
window.history.replaceState({}, document.title, "/");
const initAuth0 = async (): Promise => {
try {
// Get the client.
const initializedClient = await createClient(options);
setClient(initializedClient);
// If the user was redirect from Auth0, we need to handle the exchange or throw an error.
if (window.location.search.includes('state=') || (window.location.search.includes('error=')
|| window.location.search.includes('code='))) {
const { appState } = await initializedClient.handleRedirectCallback();
redirectAfterLogin(appState, onRedirectCallback);
}
// Authentication success.
const user = await initializedClient.getUser();
setState({
...initialState,
user,
isLoading: false,
isAuthenticated: !!user
async getAuth0Client(): Promise {
if (!this.auth0Client) {
this.auth0Client = await createAuth0Client({
domain: config.domain,
client_id: config.clientId,
audience: config.audience
});
try {
this.token.next(await this.auth0Client.getTokenSilently());
this.isAuthenticated.next(await this.auth0Client.isAuthenticated());
this.isAuthenticated.subscribe(async isAuthenticated => {
if (isAuthenticated) {
return this.profile.next(await this.auth0Client.getUser());
}
this.profile.next(null);
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(initOptions)
setAuth0(auth0FromHook)
if (window.location.search.includes('code=')) {
const { appState } = await auth0FromHook.handleRedirectCallback()
onRedirectCallback(appState)
}
const isAuthenticated = await auth0FromHook.isAuthenticated()
setIsAuthenticated(isAuthenticated)
if (isAuthenticated) {
const user = await auth0FromHook.getUser()
setUser(user)
}
async getAuth0Client(): Promise {
if (!this.auth0Client) {
this.auth0Client = await createAuth0Client({
domain: config.domain,
client_id: config.clientId
});
try {
await this.auth0Client.getTokenSilently();
this.isAuthenticated.next(await this.auth0Client.isAuthenticated());
this.isAuthenticated.subscribe(async isAuthenticated => {
if (isAuthenticated) {
return this.profile.next(await this.auth0Client.getUser());
}
this.profile.next(null);
});
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(initOptions);
setAuth0(auth0FromHook);
if (window.location.search.includes("code=")) {
const { appState } = await auth0FromHook.handleRedirectCallback();
onRedirectCallback(appState);
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(initOptions);
setAuth0(auth0FromHook);
if (window.location.search.includes('code=')) {
const { appState } = await auth0FromHook.handleRedirectCallback();
onRedirectCallback(appState);
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
const token = await auth0FromHook.getTokenSilently();
localStorage.setItem('token', token);
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(initOptions);
setAuth0(auth0FromHook);
if (window.location.search.includes("code=")) {
const { appState } = await auth0FromHook.handleRedirectCallback();
onRedirectCallback(appState);
}
const isAuthenticated = await auth0FromHook.isAuthenticated();
setIsAuthenticated(isAuthenticated);
if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}