Skip to content

Commit

Permalink
fix: pin login failures
Browse files Browse the repository at this point in the history
  • Loading branch information
uzenith360 committed Apr 29, 2024
1 parent cca0edf commit 2a7b975
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions projects/ngx-jwt-auth/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HttpHelpers } from './http-helpers';
import { EnvironmentConfig } from './environment-config.interface';
import HttpError from './http-error';
import { EnvironmentConfigService } from './environment-config.service';
import { JwtAuthService } from './jwt-auth.service';

@Injectable({
providedIn: 'root'
Expand All @@ -17,33 +18,39 @@ export class AuthService {

constructor(
@Inject(EnvironmentConfigService) private config: EnvironmentConfig,
private jwtAuthService: JwtAuthService,
private http: HttpClient,
) { }

public authWithPIN(pin: string): Observable<{ message: string, jwt: JWT }> {
return this.http.post(this.config.pinLoginUrl!, { pin })
.pipe(
HttpHelpers.retry(),
catchError(
(err, caught: Observable<{ jwt: JWT, message: string }>) => {
switch (err.status) {
case 400:
case 401:
return throwError(() => new HttpError('Login details are incorrect, use forgot password', err.status));
case 500:
return throwError(() => new HttpError('Problem logging in, please try again', err.status));
case 0:
default:
return throwError(() => new HttpError('Problem logging in, please check network and try again', err.status));
};
},
),
map(
(data: { jwt: JWT, message: string }) => {
return { message: data.message || 'Successfully logged in', jwt: data.jwt };
},
)
);
const { access_token, old_access_token, token_type } = this.jwtAuthService.getJWT()!;

return this.http.post(
this.config.pinLoginUrl!,
{ pin },
{ headers: { Authorization: `${token_type} ${access_token || old_access_token}` } }
).pipe(
HttpHelpers.retry(),
catchError(
(err, caught: Observable<{ jwt: JWT, message: string }>) => {
switch (err.status) {
case 400:
case 401:
return throwError(() => new HttpError('Login details are incorrect, use forgot password', err.status));
case 500:
return throwError(() => new HttpError('Problem logging in, please try again', err.status));
case 0:
default:
return throwError(() => new HttpError('Problem logging in, please check network and try again', err.status));
};
},
),
map(
(data: { jwt: JWT, message: string }) => {
return { message: data.message || 'Successfully logged in', jwt: data.jwt };
},
)
);
}

public auth(authId: string, password: string): Observable<{ message: string, jwt: JWT }> {
Expand Down

0 comments on commit 2a7b975

Please sign in to comment.