Hi Team,
I am totally new to auth0.
I am trying to use it for authenticate my own API using token received from auth0.
I have done following things so far
- created API
- created demon application (and test also)
- using same application’s client ID and secrete key called methods like below( have added node api project for same below)
var options = { method: ‘POST’,
url: coreConfig.url,
headers: { ‘content-type’: ‘application/json’ },
body: JSON.stringify( {“client_id”: coreConfig.clientId, “client_secret”:coreConfig.client_secret,
“audience”:coreConfig.audience,“grant_type”:coreConfig.grant_type,“prompt”:‘none’ })
};
exports.acquireHeaderInput = function (req, res) {
request(options, function (error, response, body) {
if (error) throw new Error(error);
}).pipe(res)
};
- when I call this method from postman I get proper response
- then I tried to call api method from my angular app and get response but there its giving error “Failed Silent Auth - Login required”
- I have intercepted httpclient call from my angular app to my own api. So as expected its calling auth0 but not responding with token
Please check below code and help to identify where I am making mistake.
export class InterceptorService implements HttpInterceptor {
constructor(private auth: AuthtokenService) { }
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent> {
// console.log(“Core app intercept”);
return this.auth.getTokenSilently$().pipe(
mergeMap(token => {
// console.log(token);
const tokenReq = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(tokenReq);
}),
catchError(err => throwError(err))
);
}
}
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))
);
// When calling, options can be passed if desired
// https://auth0.github.io/auth0-spa-js/classes/auth0client.html#gettokensilently
getTokenSilently$(options?): Observable {
console.log("get token silently");
// console.log(options);
console.log(this.auth0Client$.subscribe(data=>{
console.log("in");
console.log(data);
}))
console.log(this.auth0Client$);
return this.auth0Client$.pipe(
concatMap((client: Auth0Client) => from(client.getTokenSilently(options)))
);
}
in my existing service
ping$(): Observable {
return this.http.get(config.auth0endpoint +'acquireHeaderInput');
}