password change via email & token possible
This commit is contained in:
parent
052899307e
commit
966d03edbd
5 changed files with 39 additions and 5 deletions
|
|
@ -27,7 +27,7 @@ class LoginView(APIView):
|
||||||
if not user.is_active:
|
if not user.is_active:
|
||||||
return Response({'password': 'Account is inactive, please check your mails'}, status=status.HTTP_403_FORBIDDEN)
|
return Response({'password': 'Account is inactive, please check your mails'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
return self._create_token_response(user)
|
return self._create_token_response(user)
|
||||||
return Response({'detail': 'Unable to login with provided credentials.'}, status=status.HTTP_401_UNAUTHORIZED)
|
return Response({'mail': 'Unable to login with provided credentials.'}, status=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
@ -125,12 +125,15 @@ class ChangePasswordView(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
email = request.data.get('email')
|
email = request.data.get('email')
|
||||||
token = request.data.get('token')
|
token = request.data.get('token')
|
||||||
|
new_password = request.data.get('new_password')
|
||||||
|
|
||||||
if not email or not token:
|
if not email or not token:
|
||||||
return Response({"error": "Email and token are required."}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"error": "Email and token are required."}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
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:
|
||||||
|
user.set_password(new_password)
|
||||||
|
user.save()
|
||||||
return Response(status=status.HTTP_200_OK)
|
return Response(status=status.HTTP_200_OK)
|
||||||
else:
|
else:
|
||||||
return Response({"error": "Invalid email or token."}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"error": "Invalid email or token."}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ Including another URLconf
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from users import views
|
from users import views
|
||||||
from auth.views import LoginView, RegisterView, VerifyEmailView, AuthView, ForgotPasswordView
|
from auth.views import LoginView, RegisterView, VerifyEmailView, AuthView, ForgotPasswordView,ChangePasswordView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
|
@ -28,5 +28,6 @@ urlpatterns = [
|
||||||
path('auth/register/', RegisterView.as_view()),
|
path('auth/register/', RegisterView.as_view()),
|
||||||
path('auth/verify-email/', VerifyEmailView.as_view()),
|
path('auth/verify-email/', VerifyEmailView.as_view()),
|
||||||
path('auth/forgot-password/', ForgotPasswordView.as_view()),
|
path('auth/forgot-password/', ForgotPasswordView.as_view()),
|
||||||
|
path('auth/change-password/', ChangePasswordView.as_view()),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,11 @@
|
||||||
passwordConfirm.touched) {
|
passwordConfirm.touched) {
|
||||||
<p>The passwords do not match</p>
|
<p>The passwords do not match</p>
|
||||||
}
|
}
|
||||||
|
<div *ngIf="errorService.error$ | async as errors">
|
||||||
|
<div *ngIf="errors?.['error']" class="error-msg">
|
||||||
|
<p>{{ errors["error"] }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<app-btn-large
|
<app-btn-large
|
||||||
[value]="'Update Password'"
|
[value]="'Update Password'"
|
||||||
|
|
|
||||||
|
|
@ -56,9 +56,10 @@ export class ForgotPasswordComponent {
|
||||||
ngForm.form.reset();
|
ngForm.form.reset();
|
||||||
} catch {}
|
} catch {}
|
||||||
} else if (mailInput.name === 'password') {
|
} else if (mailInput.name === 'password') {
|
||||||
ngForm.form.reset();
|
try {
|
||||||
this.queryEmail = false;
|
this.changePassword();
|
||||||
this.queryEmailSuccess = true;
|
ngForm.form.reset();
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mailInput.control.markAsTouched();
|
mailInput.control.markAsTouched();
|
||||||
|
|
@ -79,6 +80,24 @@ export class ForgotPasswordComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changePassword() {
|
||||||
|
const body = {
|
||||||
|
email: this.authData.mail,
|
||||||
|
token: this.authData.token,
|
||||||
|
new_password: this.authData.password,
|
||||||
|
};
|
||||||
|
console.log(body);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.authService.changePassword(body);
|
||||||
|
this.queryEmail = false;
|
||||||
|
this.queryEmailSuccess = true;
|
||||||
|
this.errorService.clearError();
|
||||||
|
} catch (error) {
|
||||||
|
this.errorMsg(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
errorMsg(error: any) {
|
errorMsg(error: any) {
|
||||||
if (error instanceof HttpErrorResponse) {
|
if (error instanceof HttpErrorResponse) {
|
||||||
const errorTypes = ['error'];
|
const errorTypes = ['error'];
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,12 @@ export class AuthService {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async changePassword(body: any) {
|
||||||
|
await lastValueFrom(
|
||||||
|
this.http.post(`${environment.baseUrl}/auth/change-password/`, body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
storeAuthToken(data: any, storage: boolean) {
|
storeAuthToken(data: any, storage: boolean) {
|
||||||
storage
|
storage
|
||||||
? localStorage.setItem('authToken', data.toString())
|
? localStorage.setItem('authToken', data.toString())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue