Skip to content

Instantly share code, notes, and snippets.

View changhuixu's full-sized avatar
💭
Everyone has a happy ending. If you're not happy, it's not the end.

Changhui Xu changhuixu

💭
Everyone has a happy ending. If you're not happy, it's not the end.
View GitHub Profile
@NgModule({
declarations: [],
imports: [CommonModule],
providers: [
{
provide: APP_INITIALIZER,
useFactory: appInitializer,
multi: true,
deps: [AuthService],
},
export function appInitializer(authService: AuthService) {
return () =>
new Promise((resolve) => {
console.log('refresh token on app start up')
authService.refreshToken().subscribe().add(resolve);
});
}
@Injectable()
export class UnauthorizedInterceptor implements HttpInterceptor {
constructor(private authService: AuthService, private router: Router) {}
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((err) => {
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
): Observable<HttpEvent<unknown>> {
// add JWT auth header if a user is logged in for API requests
const accessToken = localStorage.getItem('access_token');
const routes: Routes = [
// ...
{ path: 'demo-apis', component: DemoApisComponent, canActivate: [AuthGuard] },
];
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
):
private storageEventListener(event: StorageEvent) {
if (event.storageArea === localStorage) {
if (event.key === 'logout-event') {
this._user.next(null);
}
if (event.key === 'login-event') {
location.reload();
}
}
}
@Injectable({
providedIn: 'root',
})
export class AuthService implements OnDestroy {
private readonly apiUrl = `${environment.apiUrl}api/account`;
private timer: Subscription;
private _user = new BehaviorSubject<ApplicationUser>(null);
user$: Observable<ApplicationUser> = this._user.asObservable();
private storageEventListener(event: StorageEvent) { // ... }
public JwtAuthResult Refresh(string refreshToken, string accessToken, DateTime now)
{
var (principal, jwtToken) = DecodeJwtToken(accessToken);
if (jwtToken == null || !jwtToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256Signature))
{
throw new SecurityTokenException("Invalid token");
}
var userName = principal.Identity.Name;
if (!_usersRefreshTokens.TryGetValue(refreshToken, out var existingRefreshToken))
[HttpPost("refresh-token")]
[Authorize]
public async Task<ActionResult> RefreshToken([FromBody] RefreshTokenRequest request)
{
try
{
var userName = User.Identity.Name;
_logger.LogInformation($"User [{userName}] is trying to refresh JWT token.");
if (string.IsNullOrWhiteSpace(request.RefreshToken))