print error messages during login/signup.

This commit is contained in:
Jason Kulatunga 2022-09-27 17:57:36 -07:00
parent 53e1574a0c
commit cb431a95ff
6 changed files with 35 additions and 13 deletions

View File

@ -22,18 +22,18 @@ export class AppComponent implements OnInit {
navbarBackdrop.classList.add('az-navbar-backdrop'); navbarBackdrop.classList.add('az-navbar-backdrop');
document.querySelector('body').appendChild(navbarBackdrop); document.querySelector('body').appendChild(navbarBackdrop);
//TODO: onfirst load the header is always shown, why?
// seems to be related to the presence of jwt token, and/or auth-interceptor.
//determine if we should show the header //determine if we should show the header
this.router.events.subscribe(event => this.modifyHeader(event)); this.router.events.subscribe(event => this.modifyHeader(event));
} }
modifyHeader(event) { modifyHeader(event) {
if(event instanceof NavigationEnd && event.url?.startsWith('/auth')) if (event instanceof NavigationEnd) {
{ if (event.url?.startsWith('/auth')) {
this.showHeader = false; this.showHeader = false;
} else { } else {
this.showHeader = true; // console.log("NU")
this.showHeader = true;
}
} }
} }
} }

View File

@ -37,6 +37,9 @@
</div><!-- form-group --> </div><!-- form-group -->
<button [disabled]="!userForm.form.valid" type="submit" class="btn btn-az-primary btn-block">Sign In</button> <button [disabled]="!userForm.form.valid" type="submit" class="btn btn-az-primary btn-block">Sign In</button>
<div *ngIf="errorMsg" class="alert alert-danger mt-3" role="alert">
<strong>Error</strong> {{errorMsg}}
</div>
</form> </form>
</div><!-- az-signin-header --> </div><!-- az-signin-header -->
<div class="az-signin-footer"> <div class="az-signin-footer">

View File

@ -11,6 +11,7 @@ import {Router} from '@angular/router';
export class AuthSigninComponent implements OnInit { export class AuthSigninComponent implements OnInit {
submitted: boolean = false submitted: boolean = false
existingUser: User = new User() existingUser: User = new User()
errorMsg: string = ""
constructor(private fastenApi: FastenApiService, private router: Router) { } constructor(private fastenApi: FastenApiService, private router: Router) { }
@ -22,8 +23,9 @@ export class AuthSigninComponent implements OnInit {
this.fastenApi.signin(this.existingUser.username, this.existingUser.password).subscribe((tokenResp: any) => { this.fastenApi.signin(this.existingUser.username, this.existingUser.password).subscribe((tokenResp: any) => {
console.log(tokenResp); console.log(tokenResp);
this.router.navigateByUrl('/dashboard'); this.router.navigateByUrl('/dashboard');
}, (err)=>{
this.errorMsg = err?.error?.error || "an unknown error occurred during sign-in"
}) })
} }
} }

View File

@ -64,6 +64,10 @@
</div> </div>
</div><!-- form-group --> </div><!-- form-group -->
<button [disabled]="!userForm.form.valid" type="submit" class="btn btn-az-primary btn-block">Create Account</button> <button [disabled]="!userForm.form.valid" type="submit" class="btn btn-az-primary btn-block">Create Account</button>
<div *ngIf="errorMsg" class="alert alert-danger mt-3" role="alert">
<strong>Error</strong> {{errorMsg}}
</div>
</form> </form>
</div><!-- az-signup-header --> </div><!-- az-signup-header -->
<div class="az-signup-footer"> <div class="az-signup-footer">

View File

@ -12,6 +12,7 @@ import {Router} from '@angular/router';
export class AuthSignupComponent implements OnInit { export class AuthSignupComponent implements OnInit {
submitted: boolean = false submitted: boolean = false
newUser: User = new User() newUser: User = new User()
errorMsg: string = ""
constructor(private fastenApi: FastenApiService, private router: Router) { } constructor(private fastenApi: FastenApiService, private router: Router) { }
@ -25,7 +26,10 @@ export class AuthSignupComponent implements OnInit {
console.log(tokenResp); console.log(tokenResp);
this.router.navigateByUrl('/dashboard'); this.router.navigateByUrl('/dashboard');
}) },
(err)=>{
this.errorMsg = err?.error?.error || "an unknown error occurred during sign-up"
})
} }
} }

View File

@ -42,8 +42,13 @@ export class FastenApiService {
signup(newUser: User): Observable<any> { signup(newUser: User): Observable<any> {
return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signup`, newUser).pipe( return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signup`, newUser).pipe(
map((res: any) => { map((res: any) => {
localStorage.setItem(this.AUTH_TOKEN_KEY, res.data); if(res.success){
return res.data localStorage.setItem(this.AUTH_TOKEN_KEY, res.data);
return res.data
} else {
throw new Error(res.error)
}
} }
)); ));
} }
@ -58,8 +63,12 @@ export class FastenApiService {
return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signin`, data).pipe( return this._httpClient.post<any>(`${this.getBasePath()}/api/auth/signin`, data).pipe(
map((res: any) => { map((res: any) => {
localStorage.setItem(this.AUTH_TOKEN_KEY, res.data); if(res.success){
return res.data localStorage.setItem(this.AUTH_TOKEN_KEY, res.data);
return res.data
} else {
throw new Error(res.error)
}
} }
)); ));
} }