NgxMaterialAuth
Provides functionality around authentication and authorization in angular.
This includes:
- A generic JwtAuthService that can easily be extended from
- Multiple Guards and HttpInterceptors that work right out of the box
- Ready to work with but highly customizable components for login, reset-password etc.
Table of Contents
Requirements
This package relies on the angular material library to render its components.
It also uses fontawesome-icons in some components.
JwtAuthService
The JwtAuthService provides functionality for most of the auth requirements:
- It handles syncing authentication data from and to localstorage
- It provides methods for login, logout, resetting the password, but also more advanced things like refreshing the current token
- Using generics you can change the type of the auth data and token data saved in local storage
It is also used in most of the other parts of the library.
In order to use it you need to extend your own service from it and register it in your app.module.ts provider array.
Usage
Define your AuthService
import { BaseAuthData, BaseToken, JwtAuthService } from 'ngx-material-auth';
@Injectable({ providedIn: 'root' })
export class CustomAuthService extends JwtAuthService<BaseAuthData, CustomRoleValues, CustomRole<CustomRoleValues>, CustomToken> {
readonly API_LOGIN_URL: string = `${environment.apiUrl}/login`;
readonly API_LOGOUT_URL: string = `${environment.apiUrl}/logout`;
readonly API_REFRESH_TOKEN_URL: string = `${environment.apiUrl}/refresh-token`;
readonly API_REQUEST_RESET_PASSWORD_URL: string = `${environment.apiUrl}/request-reset-password`;
readonly API_CONFIRM_RESET_PASSWORD_URL: string = `${environment.apiUrl}/confirm-reset-password`;
readonly API_VERIFY_RESET_PASSWORD_TOKEN_URL: string = `${environment.apiUrl}/verify-password-reset-token`;
constructor(
private readonly httpClient: HttpClient,
private readonly matSnackBar: MatSnackBar,
private readonly ngZone: NgZone
) {
super(httpClient, matSnackBar, ngZone);
}
}
As you can see, you only have to override a few urls that are used in the services login/reset-password etc. methods.
You can however also customize all other parts of the JwtAuthService.
Override the injection token
Everything else is already dealt with, all parts of NgxMaterialAuth already use that service. Now you need to provide it to them by overriding the injection token. Add the following to your app.module.ts:
import { NGX_AUTH_SERVICE } from 'ngx-material-auth';
...
providers: [
...
{
provide: NGX_AUTH_SERVICE,
useExisting: CustomAuthService
}
...
],
...
That's it! Now you are ready to use all the parts NgxMaterialAuth has to offer:
Api
JwtAuthService
export abstract class JwtAuthService<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
TokenType extends BaseToken
> {
authDataSubject: BehaviorSubject<AuthDataType | undefined>;
readonly AUTH_DATA_KEY = 'authData';
readonly ACCESS_TOKEN_DURATION_IN_MS: number = HOUR_IN_MS;
readonly REFRESH_TOKEN_DURATION_IN_MS: number = ONE_HUNDRED_DAYS_IN_MS;
readonly REQUEST_RESET_PASSWORD_SNACK_BAR_MESSAGE: string = 'A Mail for changing your password is on its way';
readonly CONFIRM_RESET_PASSWORD_SNACK_BAR_MESSAGE: string = 'Password changed successfully!';
abstract readonly API_LOGIN_URL: string;
abstract readonly API_LOGOUT_URL: string;
abstract readonly API_REFRESH_TOKEN_URL: string;
abstract readonly API_REQUEST_RESET_PASSWORD_URL: string;
abstract readonly API_CONFIRM_RESET_PASSWORD_URL: string;
abstract readonly API_VERIFY_RESET_PASSWORD_TOKEN_URL: string;
get authData(): AuthDataType | undefined {
return this.authDataSubject.value;
}
set authData(authData: AuthDataType | undefined) {
authData = this.transformAuthDataBeforeSetting(authData);
localStorage.setItem(this.AUTH_DATA_KEY, JSON.stringify(authData));
if (!authData) {
localStorage.removeItem(this.AUTH_DATA_KEY);
}
this.authDataSubject.next(authData);
}
constructor(
protected readonly http: HttpClient,
protected readonly snackbar: MatSnackBar,
protected readonly zone: NgZone
) {
const stringData = localStorage.getItem(this.AUTH_DATA_KEY);
const authData = stringData ? JSON.parse(stringData) as AuthDataType : undefined;
this.authDataSubject = new BehaviorSubject(authData);
}
protected transformAuthDataBeforeSetting(authData: AuthDataType | undefined): AuthDataType | undefined {
if (!authData) {
return undefined;
}
if (typeof authData.roles[0] === 'string') {
authData.roles = (authData.roles as unknown as RoleValue[]).map(r => {
return { displayName: r, value: r };
}) as unknown as Role[];
}
return authData;
}
async login(loginData: LoginData): Promise<AuthDataType> {
this.authData = await firstValueFrom(this.http.post<AuthDataType>(this.API_LOGIN_URL, loginData));
return this.authData;
}
async logout(): Promise<void> {
if (!this.authData) {
return;
}
await firstValueFrom(this.http.post<void>(this.API_LOGOUT_URL, { refreshToken: this.authData.refreshToken.value }));
this.authData = undefined;
}
async refreshToken(): Promise<void> {
if (!this.authData) {
return;
}
this.authData = await firstValueFrom(
this.http.post<AuthDataType>(this.API_REFRESH_TOKEN_URL, { refreshToken: this.authData.refreshToken })
);
}
async requestResetPassword(email: string): Promise<void> {
await firstValueFrom(this.http.post<void>(this.API_REQUEST_RESET_PASSWORD_URL, { email: email }));
this.zone.run(() => {
this.snackbar.open(this.REQUEST_RESET_PASSWORD_SNACK_BAR_MESSAGE);
});
}
async confirmResetPassword(newPassword: string, resetToken: string): Promise<void> {
const body = {
password: newPassword,
resetToken: resetToken
};
await firstValueFrom(this.http.post<void>(this.API_CONFIRM_RESET_PASSWORD_URL, body));
this.zone.run(() => {
this.snackbar.open(this.CONFIRM_RESET_PASSWORD_SNACK_BAR_MESSAGE);
});
}
async isResetTokenValid(resetToken: string): Promise<boolean> {
return await firstValueFrom(
this.http.post<boolean>(this.API_VERIFY_RESET_PASSWORD_TOKEN_URL, { value: resetToken })
);
}
hasRole(allowedRolesValues: RoleValue[]): boolean {
if (!this.authData) {
return false;
}
if (allowedRolesValues.find(rv => this.authData?.roles.map(r => r.value).includes(rv))) {
return true;
}
return false;
}
}
BaseToken
Can be used either directly or be extended from if your token has additional values.
export interface BaseToken {
value: string,
expirationDate: Date
}
BaseAuthData
Can be used either directly or be extended from if your authData has additional values.
export interface BaseAuthData<Token extends BaseToken, RoleValue extends string, Role extends BaseRole<RoleValue>> {
accessToken: Token,
refreshToken: Token,
roles: Role[],
userId: string
}
BaseRole
Can be used either directly or be extended from if your roles have additional values.
export interface BaseRole<RoleValue extends string> {
displayName: string,
value: RoleValue
}
Jwt Interceptor
This can be used straight out of the box if you have registered your authService.
This interceptor automatically adds the token from your AuthService to the authorization http header of every request (If a token exists).
You can (and should) provide a list of allowed domains to prohibit accidentaly sending tokens to a third party api.
It also handles the refreshing of your token if it going to run out. By default starting at 6 hours before the token expirationDate.
Usage
Add this to your app.module.ts:
import { NGX_JWT_INTERCEPTOR_ALLOWED_DOMAINS } from 'ngx-material-auth';
...
providers: [
...
{
provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true
},
{
provide: NGX_JWT_INTERCEPTOR_ALLOWED_DOMAINS, useValue: ['localhost:3000', 'example.com/api']
},
...
]
...
Api
@Injectable({ providedIn: 'root' })
export class JwtInterceptor<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements HttpInterceptor {
constructor(
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType,
@Inject(NGX_JWT_INTERCEPTOR_ALLOWED_DOMAINS)
protected readonly allowedDomains?: string[]
) {
this.allowedDomains = this.allowedDomains?.map(ad => this.getDomainFromUrl(ad));
}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
if (this.refreshTokenExpired()) {
this.authService.authData = undefined;
}
if (!this.authService.authData?.accessToken) {
return next.handle(request);
}
if (this.requestIsToDisallowedDomain(request)) {
return next.handle(request);
}
if (this.requestDoesNotRequireToken(request)) {
return next.handle(request);
}
if (this.tokenNeedsToBeRefreshed()) {
return from(this.refreshAndHandle(request, next));
}
request = request.clone({
setHeaders: {
authorization: `Bearer ${this.authService.authData.accessToken.value}`
}
});
return next.handle(request);
}
protected requestDoesNotRequireToken(request: HttpRequest<unknown>): boolean {
return request.url === this.authService.API_REFRESH_TOKEN_URL
|| request.url === this.authService.API_LOGOUT_URL;
}
protected async refreshAndHandle(request: HttpRequest<unknown>, next: HttpHandler): Promise<HttpEvent<unknown>> {
await this.authService.refreshToken();
request = request.clone({
setHeaders: {
authorization: `Bearer ${this.authService.authData?.accessToken.value}`
}
});
return await lastValueFrom(next.handle(request));
}
protected tokenNeedsToBeRefreshed(): boolean {
const tokenExpirationDate: Date = new Date(this.authService.authData?.accessToken.expirationDate as Date);
const expirationInMs: number = tokenExpirationDate.getTime();
return expirationInMs <= Date.now();
}
protected refreshTokenExpired(): boolean {
const tokenExpirationDate: Date = new Date(this.authService.authData?.refreshToken.expirationDate as Date);
const expirationInMs: number = tokenExpirationDate.getTime();
return expirationInMs <= Date.now();
}
protected requestIsToDisallowedDomain(request: HttpRequest<unknown>): boolean {
if (!this.allowedDomains) {
return false;
}
const domain = this.getDomainFromUrl(request.url);
if (this.allowedDomains.includes(domain)) {
return false;
}
return true;
}
protected getDomainFromUrl(url: string): string {
if (url.startsWith('https://')) {
url = url.split('https://')[1];
}
if (url.startsWith('http://')) {
url = url.split('http://')[1];
}
if (url.startsWith('www.')) {
url = url.split('www.')[1];
}
url = url.split('/')[0];
return url;
}
}
HTTP-Error Interceptor
This can be used straight out of the box if you have registered your authService.
This interceptor catches any error that comes from http and displays it inside of an dialog.
If the error has a specific status code (eg. 401 Unauthorized) the current user is logged out.
Usage
Add this to your app.module.ts:
...
providers: [
...
{
provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true
}
...
]
...
Api
@Injectable({ providedIn: 'root' })
export class HttpErrorInterceptor<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements HttpInterceptor {
protected readonly ROUTE_AFTER_LOGOUT = '/';
protected readonly NO_INTERNET_CONNECTION_ERROR_MESSAGE = 'No Internet Connection.<br>Please try again later.';
protected readonly CORS_ERROR_MESSAGE = 'CORS Error<br>Check your console for more information.';
protected readonly logoutStatuses: number[] = [
HttpStatusCode.Unauthorized,
HttpStatusCode.Forbidden
];
protected readonly apiUrlsWithNoLogout: string[] = [];
constructor(
protected readonly router: Router,
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType,
protected readonly dialog: MatDialog
) { }
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
if (this.userShouldBeLoggedOut(error, request)) {
void this.authService.logout().then(() => {
void this.router.navigate([this.ROUTE_AFTER_LOGOUT], {});
});
}
if (this.errorDialogShouldBeDisplayed(error, request)) {
const errorData: ErrorData = { name: 'HTTP-Error', message: this.getErrorDataMessage(error) };
this.dialog.open(NgxMatAuthErrorDialogComponent, { data: errorData, autoFocus: false, restoreFocus: false });
}
return throwError(() => error);
})
);
}
protected userShouldBeLoggedOut(error: HttpErrorResponse, request: HttpRequest<unknown>): boolean {
if (this.apiUrlsWithNoLogout.find(url => url === request.url)) {
return false;
}
return !!this.logoutStatuses.find(s => s === error.status);
}
protected errorDialogShouldBeDisplayed(error: HttpErrorResponse, request: HttpRequest<unknown>): boolean {
return true;
}
protected getErrorDataMessage(error: HttpErrorResponse): string {
if (error.error != null) {
return this.getErrorDataMessage(error.error as HttpErrorResponse);
}
if (typeof error === 'string') {
return error as string;
}
if (error.message) {
return error.message;
}
if (this.isCORSError(error)) {
if (!window.navigator.onLine) {
return this.NO_INTERNET_CONNECTION_ERROR_MESSAGE;
}
return this.CORS_ERROR_MESSAGE;
}
return JSON.stringify(error);
}
protected isCORSError(error: HttpErrorResponse): boolean {
const stringifiedError = JSON.stringify(error);
return stringifiedError === JSON.stringify({ isTrusted: true })
|| stringifiedError === JSON.stringify({ isTrusted: false });
}
}
Two Factor Interceptor
JwtLoggedInGuard
This can be used straight out of the box if you have registered your authService.
A guard that simply checks if the user is logged in or not.
Usage
Just add the guard to any route that you want to protect:
canActivate: [JwtLoggedInGuard]
Api
@Injectable({ providedIn: 'root' })
export class JwtLoggedInGuard<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements CanActivate {
protected readonly ROUTE_AFTER_LOGOUT = '/login';
protected readonly ROUTE_AFTER_REDIRECT = '/';
constructor(
protected readonly router: Router,
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.authData == null) {
if (this.userShouldBeLoggedOut(route, state)) {
void this.authService.logout().then(() => {
void this.router.navigate([this.ROUTE_AFTER_LOGOUT], {});
});
}
else {
void this.router.navigate([this.ROUTE_AFTER_REDIRECT], {});
}
return false;
}
return true;
}
protected userShouldBeLoggedOut(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true;
}
}
JwtNotLoggedInGuard
This can be used straight out of the box if you have registered your authService.
This is just the inverse of the JwtLoggedInGuard.
Usage
Just add the guard to any route that you want to protect:
canActivate: [JwtNotLoggedInGuard]
Api
@Injectable({ providedIn: 'root' })
export class JwtNotLoggedInGuard<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
TokenType extends BaseToken,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements CanActivate {
protected readonly ROUTE_AFTER_LOGOUT = '/login';
protected readonly ROUTE_AFTER_REDIRECT = '/';
constructor(
protected readonly router: Router,
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.authData != null) {
if (this.userShouldBeLoggedOut(route, state)) {
void this.authService.logout().then(() => {
void this.router.navigate([this.ROUTE_AFTER_LOGOUT], {});
});
}
else {
void this.router.navigate([this.ROUTE_AFTER_REDIRECT], {});
}
return false;
}
return true;
}
protected userShouldBeLoggedOut(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true;
}
}
JwtRoleGuard
This can be used straight out of the box if you have registered your authService.
A guard that checks if the logged in user has one of the allowed roles.
Tries to get the allowed roles from the routes data object by default.
Usage
Just add the guard to any route that you want to protect:
canActivate: [JwtRoleGuard]
Api
@Injectable({ providedIn: 'root' })
export class JwtRoleGuard<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements CanActivate {
protected readonly ROUTE_AFTER_LOGOUT = '/login';
protected readonly ROUTE_AFTER_REDIRECT = '/';
constructor(
protected readonly router: Router,
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const allowedRoles = this.getAllowedRoleValuesForRoute(route, state);
if (!this.authService.hasRole(allowedRoles)) {
if (this.userShouldBeLoggedOut(route, state)) {
void this.authService.logout().then(() => {
void this.router.navigate([this.ROUTE_AFTER_LOGOUT], {});
});
}
else {
void this.router.navigate([this.ROUTE_AFTER_REDIRECT], {});
}
return false;
}
return true;
}
protected getAllowedRoleValuesForRoute(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): RoleValue[] {
return route.data['allowedRoles'] as RoleValue[] ?? [];
}
protected userShouldBeLoggedOut(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true;
}
}
JwtBelongsToGuard
Contains base functionality to check if a user is associated with the provided route.
Needs to be overriden.
:warning:
If the accessed route already requests some data from the api which throws an Unauthorized/Forbidden Error
this can also be handled by the http-error-interceptor.
Usage
Implement your BelongsToGuard
Here you only need to override the method that decides if a user belongs to a route or not.
import { JwtBelongsToGuard } from 'ngx-material-auth';
@Injectable({ providedIn: 'root' })
export class BelongsToUserGuard extends JwtBelongsToGuard<CustomAuthData, CustomToken, CustomRoleValue, CustomRole<CustomRoleValue>, CustomAuthService> {
constructor(
private readonly angularRouter: Router,
private readonly customAuthService: CustomAuthService
) {
super(angularRouter, customAuthService);
}
protected getBelongsToForRoute(route: ActivatedRouteSnapshot): boolean {
}
}
Add it to your route
Just add the guard to any route that you want to protect:
canActivate: [BelongsToUserGuard]
Api
export abstract class JwtBelongsToGuard<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements CanActivate {
protected readonly ROUTE_AFTER_LOGOUT = '/login';
protected readonly ROUTE_AFTER_REDIRECT = '/';
constructor(
protected readonly router: Router,
protected readonly authService: AuthServiceType
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (!this.getBelongsToForRoute(route, state)) {
if (this.userShouldBeLoggedOut(route, state)) {
void this.authService.logout().then(() => {
void this.router.navigate([this.ROUTE_AFTER_LOGOUT], {});
});
}
else {
void this.router.navigate([this.ROUTE_AFTER_REDIRECT], {});
}
return false;
}
return true;
}
protected userShouldBeLoggedOut(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return true;
}
protected abstract getBelongsToForRoute(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean;
}
NgxMatAuthLoginComponent
This can be used straight out of the box if you have registered your authService.
This component provides a simple login box which is highly customizable.
It uses the login method of your auth service.
Usage
- Import NgxMatAuthLoginModule
- Use in your html:
<ngx-mat-auth-login [loginTitle]="'Custom Login'"></ngx-mat-auth-login>
Api
@Component({
selector: 'ngx-mat-auth-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class NgxMatAuthLoginComponent<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements OnInit {
@Input()
getValidationErrorMessage!: (model: NgModel) => string;
@Input()
loginTitle!: string;
@Input()
emailInputLabel!: string;
@Input()
passwordInputLabel!: string;
@Input()
loginButtonLabel!: string;
@Input()
forgotPasswordLinkData!: ForgotPasswordLinkData;
@Input()
routeAfterLogin!: string;
password?: string;
email?: string;
hide: boolean = true;
constructor(
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType,
@Inject(NGX_GET_VALIDATION_ERROR_MESSAGE)
protected readonly defaultGetValidationErrorMessage: (model: NgModel) => string,
protected readonly router: Router
) { }
ngOnInit(): void {
this.getValidationErrorMessage = this.getValidationErrorMessage ?? this.defaultGetValidationErrorMessage;
this.loginTitle = this.loginTitle ?? 'Login';
this.emailInputLabel = this.emailInputLabel ?? 'Email';
this.passwordInputLabel = this.passwordInputLabel ?? 'Password';
this.loginButtonLabel = this.loginButtonLabel ?? 'Login';
this.forgotPasswordLinkData = this.forgotPasswordLinkData ?? { displayName: 'Forgot your password?', route: '/request-reset-password' };
this.routeAfterLogin = this.routeAfterLogin ?? '/';
}
onSubmit(): void {
if (!this.email || !this.password) {
return;
}
this.authService.login({ email: this.email, password: this.password })
.then(() => {
this.email = undefined;
this.password = undefined;
void this.router.navigate([this.routeAfterLogin]);
})
.catch(err => {
this.email = undefined;
this.password = undefined;
throw err;
});
}
}
NgxMatAuthRequestResetPasswordComponent
This can be used straight out of the box if you have registered your authService.
This component provides a simple box for users to request the reset of their password.
It uses the requestResetPassword-method of your auth service.
Usage
- Import NgxMatAuthRequestResetPasswordModule
- Use in your html:
<ngx-mat-auth-request-reset-password [requestResetPasswordTitle]="'Custom'">
</ngx-mat-auth-request-reset-password>
Api
@Component({
selector: 'ngx-mat-auth-request-reset-password',
templateUrl: './request-reset-password.component.html',
styleUrls: ['./request-reset-password.component.scss']
})
export class NgxMatAuthRequestResetPasswordComponent<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements OnInit {
@Input()
getValidationErrorMessage!: (model: NgModel) => string;
@Input()
requestResetPasswordTitle!: string;
@Input()
emailInputLabel!: string;
@Input()
sendEmailButtonLabel!: string;
@Input()
cancelButtonLabel!: string;
@Input()
routeAfterRequest!: string;
email?: string;
constructor(
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType,
@Inject(NGX_GET_VALIDATION_ERROR_MESSAGE)
protected readonly defaultGetValidationErrorMessage: (model: NgModel) => string,
protected readonly router: Router
) { }
ngOnInit(): void {
this.requestResetPasswordTitle = this.requestResetPasswordTitle ?? 'Forgot Password';
this.getValidationErrorMessage = this.getValidationErrorMessage ?? this.defaultGetValidationErrorMessage;
this.emailInputLabel = this.emailInputLabel ?? 'Email';
this.sendEmailButtonLabel = this.sendEmailButtonLabel ?? 'Send Email';
this.cancelButtonLabel = this.cancelButtonLabel ?? 'Cancel';
this.routeAfterRequest = this.routeAfterRequest ?? '/login';
}
cancel(): void {
void this.router.navigate([this.routeAfterRequest]);
}
onSubmit(): void {
if (!this.email) {
return;
}
this.authService.requestResetPassword(this.email)
.then(() => {
this.email = undefined;
void this.router.navigate([this.routeAfterRequest]);
})
.catch(err => {
this.email = undefined;
throw err;
});
}
}
NgxMatAuthConfirmResetPasswordComponent
This can be used straight out of the box if you have registered your authService.
This component provides a simple box for users input their new password after it has been requested.
This also checks if the provided reset token is correct. The reset token needs to be available from route.params['token']
.
It uses the requestResetPassword-method of your auth service.
Usage
- Import NgxMatAuthConfirmResetPasswordModule
- Use in your html:
<ngx-mat-auth-confirm-reset-password [confirmResetPasswordTitle]="'Custom Title'">
</ngx-mat-auth-confirm-reset-password>
Api
@Component({
selector: 'ngx-mat-auth-confirm-reset-password',
templateUrl: './confirm-reset-password.component.html',
styleUrls: ['./confirm-reset-password.component.scss']
})
export class NgxMatAuthConfirmResetPasswordComponent<
AuthDataType extends BaseAuthData<TokenType, RoleValue, Role>,
TokenType extends BaseToken,
RoleValue extends string,
Role extends BaseRole<RoleValue>,
AuthServiceType extends JwtAuthService<AuthDataType, RoleValue, Role, TokenType>
> implements OnInit {
@Input()
getValidationErrorMessage!: (model: NgModel) => string;
@Input()
confirmResetPasswordTitle!: string;
@Input()
passwordInputLabel!: string;
@Input()
confirmPasswordInputLabel!: string;
@Input()
changePasswordButtonLabel!: string;
@Input()
cancelButtonLabel!: string;
@Input()
routeForCancel!: string;
@Input()
routeAfterReset!: string;
@Input()
routeIfResetTokenInvalid!: string;
@Input()
invalidResetTokenErrorData!: ErrorData;
password?: string;
confirmPassword?: string;
hide: boolean = true;
hideConfirm: boolean = true;
private resetToken?: string;
private readonly defaultInvalidResetTokenErrorData: ErrorData = {
name: 'Error',
message: '<p>The provided link is no longer active.</p><p>Please check if the url is correct or request a new link.</p>'
};
constructor(
@Inject(NGX_AUTH_SERVICE)
protected readonly authService: AuthServiceType,
@Inject(NGX_GET_VALIDATION_ERROR_MESSAGE)
protected readonly defaultGetValidationErrorMessage: (model: NgModel) => string,
protected readonly router: Router,
protected readonly route: ActivatedRoute,
protected readonly zone: NgZone,
protected readonly dialog: MatDialog
) { }
async ngOnInit(): Promise<void> {
this.initDefaultValues();
this.resetToken = (await firstValueFrom(this.route.params))['token'] as string | undefined;
if (
!this.resetToken
|| !(await this.authService.isResetTokenValid(this.resetToken))
) {
await this.router.navigate([this.routeIfResetTokenInvalid]);
this.zone.run(() => {
this.dialog.open(
NgxMatAuthErrorDialogComponent,
{ data: this.invalidResetTokenErrorData, autoFocus: false, restoreFocus: false }
);
});
return;
}
}
private initDefaultValues(): void {
this.getValidationErrorMessage = this.getValidationErrorMessage ?? this.defaultGetValidationErrorMessage;
this.confirmResetPasswordTitle = this.confirmResetPasswordTitle ?? 'New Password';
this.passwordInputLabel = this.passwordInputLabel ?? 'Password';
this.confirmPasswordInputLabel = this.confirmPasswordInputLabel ?? 'Confirm Password';
this.changePasswordButtonLabel = this.changePasswordButtonLabel ?? 'Change Password';
this.cancelButtonLabel = this.cancelButtonLabel ?? 'Cancel';
this.routeAfterReset = this.routeAfterReset ?? '/login';
this.routeIfResetTokenInvalid = this.routeIfResetTokenInvalid ?? '/';
this.routeForCancel = this.routeForCancel ?? this.routeAfterReset;
this.invalidResetTokenErrorData = this.invalidResetTokenErrorData ?? this.defaultInvalidResetTokenErrorData;
}
inputInvalid(): boolean {
if (!this.password) {
return true;
}
if (this.password !== this.confirmPassword) {
return true;
}
return false;
}
cancel(): void {
void this.router.navigate([this.routeForCancel]);
}
onSubmit(): void {
if (!this.password) {
return;
}
if (this.password !== this.confirmPassword) {
return;
}
this.authService.confirmResetPassword(this.password, this.resetToken as string)
.then(() => {
this.resetInputFields();
void this.router.navigate([this.routeAfterReset]);
})
.catch(() => {
this.resetInputFields();
void this.router.navigate([this.routeAfterReset]);
});
}
private resetInputFields(): void {
this.password = '';
this.confirmPassword = '';
this.resetToken = '';
}
}
NgxMatAuthErrorDialogComponent
This can be used straight out of the box.
This component provides a generic dialog to display error messages.
It is used internally by the framework eg. to display error messages from the http-error-interceptor.
Error Data
You need to provide an errorData-object to the dialog in order for it to work.
This is a really simple model:
export interface ErrorData {
name: string,
message: string
Usage
Wherever you want to display the dialog:
import { NgxMatAuthErrorDialogComponent, ErrorData } from 'ngx-material-auth';
...
constructor(
private readonly dialog: MatDialog
) { }
...
this.dialog.open(NgxMatAuthErrorDialogComponent, { data: errorData });
Api
@Component({
selector: 'ngx-material-auth-error-dialog',
templateUrl: './error-dialog.component.html',
styleUrls: ['./error-dialog.component.scss'],
standalone: true,
imports: [
MatButtonModule
]
})
export class NgxMatAuthErrorDialogComponent {
constructor(
public dialogRef: MatDialogRef<NgxMatAuthErrorDialogComponent>,
@Inject(MAT_DIALOG_DATA) public error: ErrorData,
) { }
close(): void {
this.dialogRef.close();
}
}