error when the email does not exist & clean code error service
This commit is contained in:
parent
966d03edbd
commit
bfa8b61449
7 changed files with 37 additions and 44 deletions
|
|
@ -40,7 +40,7 @@ class RegisterView(APIView):
|
||||||
email = request.data.get('email')
|
email = request.data.get('email')
|
||||||
password = request.data.get('password')
|
password = request.data.get('password')
|
||||||
if CustomUser.objects.filter(email=email).exists():
|
if CustomUser.objects.filter(email=email).exists():
|
||||||
return Response({'detail': 'A user with this email already exists.'}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'mail': 'A user with this email already exists.'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
serialized = UserSerializer(data=request.data)
|
serialized = UserSerializer(data=request.data)
|
||||||
if serialized.is_valid():
|
if serialized.is_valid():
|
||||||
|
|
@ -81,7 +81,7 @@ class VerifyEmailView(APIView):
|
||||||
user = CustomUser.objects.filter(Q(email=email) & Q(verify_email_token=token)).first()
|
user = CustomUser.objects.filter(Q(email=email) & Q(verify_email_token=token)).first()
|
||||||
if user:
|
if user:
|
||||||
if user.is_active:
|
if user.is_active:
|
||||||
return Response({"error": "User has already been activated"}, status=status.HTTP_409_CONFLICT)
|
return Response({"error": "User has already been activated."}, status=status.HTTP_409_CONFLICT)
|
||||||
|
|
||||||
user.is_active = True
|
user.is_active = True
|
||||||
user.save()
|
user.save()
|
||||||
|
|
@ -93,7 +93,7 @@ class ForgotPasswordView(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
email = request.data.get('email')
|
email = request.data.get('email')
|
||||||
if not email:
|
if not email:
|
||||||
return Response({"error": "Email required."}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"mail": "Email required."}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user = CustomUser.objects.get(email=email)
|
user = CustomUser.objects.get(email=email)
|
||||||
|
|
@ -101,7 +101,7 @@ class ForgotPasswordView(APIView):
|
||||||
user.save()
|
user.save()
|
||||||
self.send_email(user)
|
self.send_email(user)
|
||||||
except CustomUser.DoesNotExist:
|
except CustomUser.DoesNotExist:
|
||||||
pass
|
return Response({"mail": "This email does not exist."}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
def send_email(self, user):
|
def send_email(self, user):
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
!isUserEmailValid(authData.mail.toLowerCase()) &&
|
!isUserEmailValid(authData.mail.toLowerCase()) &&
|
||||||
authData.mail.length > 0)
|
authData.mail.length > 0)
|
||||||
"
|
"
|
||||||
|
(click)="this.errorService.clearError()"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
<div class="error-msg">
|
<div class="error-msg">
|
||||||
|
|
@ -34,6 +35,11 @@
|
||||||
!isUserEmailValid(authData.mail.toLowerCase())) {
|
!isUserEmailValid(authData.mail.toLowerCase())) {
|
||||||
<p>This is not a valid email format</p>
|
<p>This is not a valid email format</p>
|
||||||
}}
|
}}
|
||||||
|
<div *ngIf="errorService.error$ | async as errors">
|
||||||
|
<div *ngIf="errors?.['mail']">
|
||||||
|
<p>{{ errors["mail"] }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<app-btn-large
|
<app-btn-large
|
||||||
[value]="'Send Email'"
|
[value]="'Send Email'"
|
||||||
|
|
|
||||||
|
|
@ -48,16 +48,16 @@ export class ForgotPasswordComponent {
|
||||||
return emailRegex.test(emailValue);
|
return emailRegex.test(emailValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
onSubmit(ngForm: NgForm, mailInput: any) {
|
async onSubmit(ngForm: NgForm, mailInput: any) {
|
||||||
if (ngForm.submitted && ngForm.form.valid) {
|
if (ngForm.submitted && ngForm.form.valid) {
|
||||||
if (mailInput.name === 'mail') {
|
if (mailInput.name === 'mail') {
|
||||||
try {
|
try {
|
||||||
this.verifyEmail();
|
await this.verifyEmail();
|
||||||
ngForm.form.reset();
|
ngForm.form.reset();
|
||||||
} catch {}
|
} catch {}
|
||||||
} else if (mailInput.name === 'password') {
|
} else if (mailInput.name === 'password') {
|
||||||
try {
|
try {
|
||||||
this.changePassword();
|
await this.changePassword();
|
||||||
ngForm.form.reset();
|
ngForm.form.reset();
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
@ -76,7 +76,7 @@ export class ForgotPasswordComponent {
|
||||||
this.errorService.clearError();
|
this.errorService.clearError();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.sendMailSuccess = false;
|
this.sendMailSuccess = false;
|
||||||
this.errorMsg(error);
|
this.errorService.errorMsg(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -94,20 +94,7 @@ export class ForgotPasswordComponent {
|
||||||
this.queryEmailSuccess = true;
|
this.queryEmailSuccess = true;
|
||||||
this.errorService.clearError();
|
this.errorService.clearError();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.errorMsg(error);
|
this.errorService.errorMsg(error);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
errorMsg(error: any) {
|
|
||||||
if (error instanceof HttpErrorResponse) {
|
|
||||||
const errorTypes = ['error'];
|
|
||||||
for (const type of errorTypes) {
|
|
||||||
if (error.error[type]) {
|
|
||||||
this.errorService.setError(type, error.error[type]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.errorService.clearError();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,21 +45,8 @@ export class LoginComponent {
|
||||||
this.router.navigate(['/browse/']);
|
this.router.navigate(['/browse/']);
|
||||||
this.errorService.clearError();
|
this.errorService.clearError();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.errorMsg(error);
|
this.errorService.errorMsg(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errorMsg(error: any) {
|
|
||||||
if (error instanceof HttpErrorResponse) {
|
|
||||||
const errorTypes = ['mail', 'password'];
|
|
||||||
for (const type of errorTypes) {
|
|
||||||
if (error.error[type]) {
|
|
||||||
this.errorService.setError(type, error.error[type]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.errorService.clearError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,9 +38,12 @@
|
||||||
} @else { @if (mail.touched && authData.mail.length > 0 &&
|
} @else { @if (mail.touched && authData.mail.length > 0 &&
|
||||||
!isUserEmailValid(authData.mail.toLowerCase())) {
|
!isUserEmailValid(authData.mail.toLowerCase())) {
|
||||||
<p>This is not a valid email format</p>
|
<p>This is not a valid email format</p>
|
||||||
}} @if ((errorService.error$ | async)) {
|
}}
|
||||||
<p>{{ errorService.error$ | async }}</p>
|
<div *ngIf="errorService.error$ | async as errors">
|
||||||
}
|
<div *ngIf="errors?.['mail']" class="error-msg">
|
||||||
|
<p>{{ errors["mail"] }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
|
|
|
||||||
|
|
@ -55,11 +55,7 @@ export class RegisterComponent {
|
||||||
this.registrationSuccess = true;
|
this.registrationSuccess = true;
|
||||||
this.errorService.clearError();
|
this.errorService.clearError();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// if (error instanceof HttpErrorResponse) {
|
this.errorService.errorMsg(error);
|
||||||
// this.errorService.setError(error.error.detail);
|
|
||||||
// } else {
|
|
||||||
// this.errorService.setError('An unknown error has occurred.');
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { HttpErrorResponse } from '@angular/common/http';
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { BehaviorSubject, Observable } from 'rxjs';
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
|
|
@ -32,4 +33,17 @@ export class ErrorService {
|
||||||
clearError() {
|
clearError() {
|
||||||
this.errorSubject.next(null);
|
this.errorSubject.next(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
errorMsg(error: any) {
|
||||||
|
if (error instanceof HttpErrorResponse) {
|
||||||
|
const errorTypes = ['mail', 'password', 'error'];
|
||||||
|
for (const type of errorTypes) {
|
||||||
|
if (error.error[type]) {
|
||||||
|
this.setError(type, error.error[type]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.clearError();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue