Регистрация при помощи jwt angular
Новичок в ангуляре, не могу разобраться, как зарегистрироваться при помощи токена (jwt). Что я упускаю, что нужно добавить? Прилетает ошибка 422.
auth.actions.ts:
export class SignUp {
public static readonly type = '[Auth] SignUp';
constructor(public readonly name: string, public readonly password: string, public readonly phoneNumber: string) {}
}
export class SignUpSuccess {
public static readonly type = '[Auth] SignUpSuccess';
constructor(public readonly employee: IEmployee) {}
}
export class SignUpFailed {
public static readonly type = '[Auth] SignUpFailed';
constructor(public readonly error: any) {}
}
auth.service.ts:
public signUp(name: string, password: string, phoneNumber: string): Observable<IEmployee> {
return this.http.post<IEmployee>(`${environment.apiUrl}/sign_up.json`, {name, password, phoneNumber });
}
auth.state.ts:
@Action(SignUp)
public signUp(ctx: StateContext<AuthStateModel>, { name, password, phoneNumber }: SignUp) {
return this.authService.signUp(name, password, phoneNumber).pipe(
filter(employee => !!employee),
tap((employee: IEmployee) => ctx.dispatch(new SignUpSuccess(employee))),
catchError(error => {
ctx.dispatch(new SignUpFailed(error));
return throwError(error);
})
);
}
auth.interceptor.ts:
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Skip third-party requests
if (!request.url.includes(environment.apiUrl)) {
return next.handle(request);
}
const employee = this.store.selectSnapshot(AuthState.employee);
if (employee?.token) {
// clone and modify the request
request = request.clone({
setHeaders: {
Authorization: `Bearer ${employee.token}`
}
});
}
return next.handle(request);
}
sign-up.page.ts
public onRegistration() {
const { name, password, phoneNumber } = this.formRegistration.value;
this.store.dispatch(new SignUp(name, password, phoneNumber ));
}