diff options
| author | transtrike <transtrike@gmail.com> | 2021-02-12 19:04:53 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-02-12 19:04:53 +0200 |
| commit | bcd88af53b1a920d728ec98b45daa9ac2e2c0917 (patch) | |
| tree | fd27eef086822b0f02f74364cac0b940956d2458 /src/app/components/login | |
| parent | 1d1f05e3f74d70a558b4847a9107afa7952131cf (diff) | |
| download | DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.tar DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.tar.gz DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.zip | |
Moved from DevHive
Diffstat (limited to 'src/app/components/login')
| -rw-r--r-- | src/app/components/login/login.component.css | 32 | ||||
| -rw-r--r-- | src/app/components/login/login.component.html | 30 | ||||
| -rw-r--r-- | src/app/components/login/login.component.ts | 59 |
3 files changed, 121 insertions, 0 deletions
diff --git a/src/app/components/login/login.component.css b/src/app/components/login/login.component.css new file mode 100644 index 0000000..766522e --- /dev/null +++ b/src/app/components/login/login.component.css @@ -0,0 +1,32 @@ +* { + transition: .2s; +} + +form { + width: 100%; +} + +#content hr { + width: 100%; + border: 1px solid black; + box-sizing: border-box; +} + +.input-selection:nth-of-type(1) { + margin-top: 1.2em; +} + +.submit-btn { + margin-bottom: .2em; +} + +.redirect-to-register { + color: var(--focus-color); + background-color: var(--bg-color); + border-color: var(--focus-color); +} + +.redirect-to-register:hover { + border-color: black !important; + color: black; +} diff --git a/src/app/components/login/login.component.html b/src/app/components/login/login.component.html new file mode 100644 index 0000000..13f9bbf --- /dev/null +++ b/src/app/components/login/login.component.html @@ -0,0 +1,30 @@ +<div id="content"> + <div class="title">Login</div> + + <form [formGroup]="loginUserFormGroup" (ngSubmit)="onSubmit()"> + <hr> + + <div class="input-selection"> + <input type="text" placeholder="Username" class="input-field" formControlName="username" required> + <label class="input-field-label">Username</label> + + <div class="input-errors"> + <label *ngIf="loginUserFormGroup.get('username')?.errors?.required" class="error">*Required</label> + </div> + </div> + + <div class="input-selection"> + <input type="password" placeholder="Password" class="input-field" formControlName="password" required> + <label class="input-field-label">Password</label> + + <div class="input-errors"> + <label *ngIf="loginUserFormGroup.get('password')?.errors?.required" class="error">*Required</label> + </div> + </div> + + <hr> + <button class="submit-btn" type="submit">Submit</button> + <app-error-bar></app-error-bar> + </form> + <button class="submit-btn redirect-to-register" (click)="onRedirectRegister()">New to DevHive? Register instead</button> +</div> diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts new file mode 100644 index 0000000..c3fb79c --- /dev/null +++ b/src/app/components/login/login.component.ts @@ -0,0 +1,59 @@ +import { Component, OnInit, ViewChild } from '@angular/core'; +import { FormGroup, FormBuilder, Validators, FormControl, AbstractControl } from '@angular/forms'; +import { Router } from '@angular/router'; +import { Title } from '@angular/platform-browser'; +import { UserService } from 'src/app/services/user.service'; +import { HttpErrorResponse } from '@angular/common/http'; +import { ErrorBarComponent } from '../error-bar/error-bar.component'; +import { TokenService } from 'src/app/services/token.service'; + +@Component({ + selector: 'app-login', + templateUrl: './login.component.html', + styleUrls: ['./login.component.css'] +}) +export class LoginComponent implements OnInit { + @ViewChild(ErrorBarComponent) private _errorBar: ErrorBarComponent; + private _title = 'Login'; + public loginUserFormGroup: FormGroup; + + constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _tokenService: TokenService) { + this._titleService.setTitle(this._title); + } + + ngOnInit(): void { + this.loginUserFormGroup = this._fb.group({ + username: new FormControl('', [ + Validators.required + ]), + password: new FormControl('', [ + Validators.required + ]) + }); + } + + onSubmit(): void { + this._errorBar.hideError(); + this._userService.loginUserRequest(this.loginUserFormGroup).subscribe( + (res: object) => { + this._tokenService.setUserTokenToSessionStorage(res); + this._router.navigate(['/']); + }, + (err: HttpErrorResponse) => { + this._errorBar.showError(err); + } + ); + } + + onRedirectRegister(): void { + this._router.navigate(['/register']); + } + + get username(): AbstractControl | null { + return this.loginUserFormGroup.get('username'); + } + + get password(): AbstractControl | null { + return this.loginUserFormGroup.get('password'); + } +} |
