switchable password visibility

This commit is contained in:
Chneemann 2024-08-10 07:56:28 +02:00
parent 1f460dc0a4
commit d7b07202ec
10 changed files with 156 additions and 80 deletions

View file

@ -66,21 +66,28 @@
(ngSubmit)="onSubmit(taskForm, password)"
onsubmit="return false"
>
<input
type="password"
id="password"
name="password"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<div class="password-field">
<input
[type]="authService.passwordFieldType"
id="password"
name="password"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<img
class="passwordEye"
(click)="authService.togglePasswordVisibility()"
[src]="authService.passwordIcon"
/>
</div>
<div class="error-msg">
@if (!password.valid && password.touched && authData.password.length <
1) {
@ -90,22 +97,29 @@
<p>Password is too short, min 6 characters</p>
} }
</div>
<input
type="password"
id="passwordConfirm"
name="passwordConfirm"
#passwordConfirm="ngModel"
placeholder="Confirm password"
autocomplete="new-password"
[(ngModel)]="authData.passwordConfirm"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
authData.password !== authData.passwordConfirm &&
passwordConfirm.touched
"
required
/>
<div class="password-field">
<input
[type]="authService.passwordFieldType"
id="passwordConfirm"
name="passwordConfirm"
#passwordConfirm="ngModel"
placeholder="Confirm password"
autocomplete="new-password"
[(ngModel)]="authData.passwordConfirm"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
authData.password !== authData.passwordConfirm &&
passwordConfirm.touched
"
required
/>
<img
class="passwordEye"
(click)="authService.togglePasswordVisibility()"
[src]="authService.passwordIcon"
/>
</div>
<div class="error-msg">
@if (authData.password !== authData.passwordConfirm &&
passwordConfirm.touched) {

View file

@ -28,7 +28,7 @@ export class ForgotPasswordComponent {
constructor(
private route: ActivatedRoute,
private authService: AuthService,
public authService: AuthService,
public errorService: ErrorService
) {}

View file

@ -31,21 +31,29 @@
<p>This is not a valid email format</p>
}}
</div>
<input
type="password"
id="password"
name="password"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<div class="password-field">
<input
[type]="authService.passwordFieldType"
id="password"
name="password"
class="password-input"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<img
class="passwordEye"
(click)="authService.togglePasswordVisibility()"
[src]="authService.passwordIcon"
/>
</div>
<div class="error-msg">
@if (!password.valid && password.touched && authData.password.length <
1) {

View file

@ -22,8 +22,10 @@ export class LoginComponent {
send: false,
};
showPassword: boolean = false;
constructor(
private authService: AuthService,
public authService: AuthService,
public errorService: ErrorService,
private router: Router
) {}

View file

@ -40,21 +40,28 @@
<p>This is not a valid email format</p>
}}
</div>
<input
type="password"
id="password"
name="password"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<div class="password-field">
<input
[type]="authService.passwordFieldType"
id="password"
name="password"
#password="ngModel"
placeholder="Enter a password"
autocomplete="new-password"
[(ngModel)]="authData.password"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
password.invalid && password.touched && authData.password.length < 6
"
required
/>
<img
class="passwordEye"
(click)="authService.togglePasswordVisibility()"
[src]="authService.passwordIcon"
/>
</div>
<div class="error-msg">
@if (!password.valid && password.touched && authData.password.length <
1) {
@ -64,22 +71,29 @@
<p>Password is too short, min 6 characters</p>
} }
</div>
<input
type="password"
id="passwordConfirm"
name="passwordConfirm"
#passwordConfirm="ngModel"
placeholder="Confirm password"
autocomplete="new-password"
[(ngModel)]="authData.passwordConfirm"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
authData.password !== authData.passwordConfirm &&
passwordConfirm.touched
"
required
/>
<div class="password-field">
<input
[type]="authService.passwordFieldType"
id="passwordConfirm"
name="passwordConfirm"
#passwordConfirm="ngModel"
placeholder="Confirm password"
autocomplete="new-password"
[(ngModel)]="authData.passwordConfirm"
pattern="[^<>]*"
minlength="6"
[class.error-border]="
authData.password !== authData.passwordConfirm &&
passwordConfirm.touched
"
required
/>
<img
class="passwordEye"
(click)="authService.togglePasswordVisibility()"
[src]="authService.passwordIcon"
/>
</div>
<div class="error-msg">
@if (authData.password !== authData.passwordConfirm &&
passwordConfirm.touched) {

View file

@ -26,7 +26,7 @@ export class RegisterComponent {
constructor(
private route: ActivatedRoute,
private authService: AuthService,
public authService: AuthService,
public errorService: ErrorService,
private router: Router
) {}

View file

@ -8,9 +8,24 @@ import { environment } from '../environments/environment.development';
})
export class AuthService {
errorMsg: string | null = null;
passwordFieldType: string = 'password';
passwordIcon: string = './../../../assets/img/close-eye.svg';
constructor(private http: HttpClient) {}
togglePasswordVisibility() {
this.passwordFieldType =
this.passwordFieldType === 'password' ? 'text' : 'password';
this.toggleIcon();
}
toggleIcon() {
this.passwordIcon =
this.passwordIcon === './../../../assets/img/close-eye.svg'
? './../../../assets/img/open-eye.svg'
: './../../../assets/img/close-eye.svg';
}
async register(body: any) {
await lastValueFrom(
this.http.post(`${environment.baseUrl}/auth/register/`, body)

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#434343"><path d="m644-428-58-58q9-47-27-88t-93-32l-58-58q17-8 34.5-12t37.5-4q75 0 127.5 52.5T660-500q0 20-4 37.5T644-428Zm128 126-58-56q38-29 67.5-63.5T832-500q-50-101-143.5-160.5T480-720q-29 0-57 4t-55 12l-62-62q41-17 84-25.5t90-8.5q151 0 269 83.5T920-500q-23 59-60.5 109.5T772-302Zm20 246L624-222q-35 11-70.5 16.5T480-200q-151 0-269-83.5T40-500q21-53 53-98.5t73-81.5L56-792l56-56 736 736-56 56ZM222-624q-29 26-53 57t-41 67q50 101 143.5 160.5T480-280q20 0 39-2.5t39-5.5l-36-38q-11 3-21 4.5t-21 1.5q-75 0-127.5-52.5T300-500q0-11 1.5-21t4.5-21l-84-82Zm319 93Zm-151 75Z"/></svg>

After

Width:  |  Height:  |  Size: 675 B

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#434343"><path d="M480-320q75 0 127.5-52.5T660-500q0-75-52.5-127.5T480-680q-75 0-127.5 52.5T300-500q0 75 52.5 127.5T480-320Zm0-72q-45 0-76.5-31.5T372-500q0-45 31.5-76.5T480-608q45 0 76.5 31.5T588-500q0 45-31.5 76.5T480-392Zm0 192q-146 0-266-81.5T40-500q54-137 174-218.5T480-800q146 0 266 81.5T920-500q-54 137-174 218.5T480-200Zm0-300Zm0 220q113 0 207.5-59.5T832-500q-50-101-144.5-160.5T480-720q-113 0-207.5 59.5T128-500q50 101 144.5 160.5T480-280Z"/></svg>

After

Width:  |  Height:  |  Size: 554 B

View file

@ -45,6 +45,23 @@ form {
}
}
// Show / Hide Password
.password-field {
position: relative;
width: 100%;
}
.passwordEye {
position: absolute;
right: 48px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
filter: brightness(0) saturate(100%) invert(16%) sepia(77%) saturate(7494%)
hue-rotate(241deg) brightness(93%) contrast(88%);
}
/*------------- RESPONSIVE -------------*/
@media screen and (max-width: 600px) {
@ -53,4 +70,8 @@ form {
width: 100%;
}
}
.passwordEye {
right: 18px;
}
}