Update/fix login

This commit is contained in:
phil 2024-03-16 11:19:21 +05:30
parent d51e597376
commit 8593d0b5cd
2 changed files with 10 additions and 13 deletions

View file

@ -54,17 +54,13 @@ export class AuthenticationService {
) )
} }
login(userName: string, password: string): Observable<boolean> { login(userName: string, password: string): Observable<AuthResponse> {
let body = JSON.stringify({ const headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'})
userName: userName, var formData: any = new URLSearchParams()
password: password formData.set('username', userName)
}) formData.set('password', password)
return this._http.post<AuthResponse>( return this._http.post<AuthResponse>(
'/auth/login', '/api/token', formData, {headers: headers}
body,
{
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
}
).pipe(map( ).pipe(map(
(response: AuthResponse) => { (response: AuthResponse) => {
// login successful if there's a jwt token in the response // login successful if there's a jwt token in the response
@ -87,13 +83,14 @@ export class AuthenticationService {
this.user.next(new User(userName, token)) this.user.next(new User(userName, token))
// return true to indicate successful login // return true to indicate successful login
return true // return true
} else { } else {
this.user.next(undefined) this.user.next(undefined)
this.roles = [] this.roles = []
// return false to indicate failed login // return false to indicate failed login
return false // return false
} }
return response
} }
)) ))
} }

View file

@ -41,7 +41,7 @@ export class LoginComponent implements OnInit {
this.loading = true this.loading = true
this.authenticationService.login(this.formGroup.value.userName, this.formGroup.value.password).subscribe({ this.authenticationService.login(this.formGroup.value.userName, this.formGroup.value.password).subscribe({
next: result => { next: result => {
if (result === true) { if (result.access_token) {
// login successful // login successful
this.router.navigate(['/']) this.router.navigate(['/'])
} else { } else {