-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a dual-purpose login/register page and guarded JournalPage from unauthenticated Users. included menu with logout button.
- Loading branch information
Showing
12 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { Routes, RouterModule } from '@angular/router'; | ||
|
||
import { LoginPage } from './login.page'; | ||
|
||
const routes: Routes = [ | ||
{ | ||
path: '', | ||
component: LoginPage | ||
} | ||
]; | ||
|
||
@NgModule({ | ||
imports: [RouterModule.forChild(routes)], | ||
exports: [RouterModule], | ||
}) | ||
export class LoginPageRoutingModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { FormsModule } from '@angular/forms'; | ||
|
||
import { IonicModule } from '@ionic/angular'; | ||
|
||
import { LoginPageRoutingModule } from './login-routing.module'; | ||
|
||
import { LoginPage } from './login.page'; | ||
import { SharedModule } from 'src/app/shared/shared.module'; | ||
|
||
@NgModule({ | ||
imports: [SharedModule, LoginPageRoutingModule], | ||
declarations: [LoginPage], | ||
}) | ||
export class LoginPageModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<ion-header> | ||
<ion-toolbar> | ||
<ion-title>Authenticate yourself!</ion-title> | ||
</ion-toolbar> | ||
</ion-header> | ||
|
||
<ion-content> | ||
<form [formGroup]="form" action="submit"> | ||
<ion-list> | ||
<ion-item> | ||
<ion-label>Email:</ion-label> | ||
<ion-input | ||
formControlName="email" | ||
type="email" | ||
placeholder="email@domain.com" | ||
></ion-input> | ||
</ion-item> | ||
<ion-item> | ||
<ion-label>Password:</ion-label> | ||
<ion-input formControlName="password" type="password"></ion-input> | ||
</ion-item> | ||
</ion-list> | ||
<ion-button | ||
expand="block" | ||
[disabled]="form.invalid" | ||
(click)="login()" | ||
color="primary" | ||
>Log In</ion-button | ||
> | ||
<hr /> | ||
<ion-button | ||
expand="block" | ||
[disabled]="form.invalid" | ||
(click)="register()" | ||
color="secondary" | ||
>Register</ion-button | ||
> | ||
</form> | ||
</ion-content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
form { | ||
margin: auto; | ||
max-width: 500px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { IonicModule } from '@ionic/angular'; | ||
|
||
import { LoginPage } from './login.page'; | ||
|
||
describe('LoginPage', () => { | ||
let component: LoginPage; | ||
let fixture: ComponentFixture<LoginPage>; | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ LoginPage ], | ||
imports: [IonicModule.forRoot()] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(LoginPage); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
})); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// NOTE this is a dual purpose login/register page. Usually you'd split the two obviously but for the sake of time... | ||
|
||
import { Component, OnInit } from '@angular/core'; | ||
import { FormControl, FormGroup, Validators } from '@angular/forms'; | ||
import { Router } from '@angular/router'; | ||
import { AlertController } from '@ionic/angular'; | ||
import { AuthService } from 'src/app/services/auth.service'; | ||
import { LoadingService } from 'src/app/services/loading.service'; | ||
|
||
@Component({ | ||
selector: 'app-login', | ||
templateUrl: './login.page.html', | ||
styleUrls: ['./login.page.scss'], | ||
}) | ||
export class LoginPage implements OnInit { | ||
form = new FormGroup({ | ||
email: new FormControl<string>('', [Validators.required, Validators.email]), | ||
password: new FormControl<string>('', [ | ||
Validators.required, | ||
Validators.minLength(8), | ||
]), | ||
}); | ||
|
||
constructor( | ||
private auth: AuthService, | ||
private router: Router, | ||
private loadingService: LoadingService, | ||
private alert: AlertController | ||
) {} | ||
|
||
ngOnInit() {} | ||
|
||
async login() { | ||
await this.loadingService.create('Logging you in...'); | ||
try { | ||
const user = await this.auth.login( | ||
this.form.value as { email: string; password: string } | ||
); | ||
this.loadingService.dismiss(); | ||
this.router.navigate(['/']); | ||
} catch (error) { | ||
// TODO proper handling of this error, give the Users an explanation/way to resolve | ||
this.loadingService.dismiss(); | ||
const alert = await this.alert.create({ | ||
message: 'Failed to log you in. Check your credentials.', | ||
buttons: ['OK'], | ||
}); | ||
alert.present(); | ||
} | ||
} | ||
|
||
async register() { | ||
await this.loadingService.create('Signing you up...'); | ||
try { | ||
const user = await this.auth.register( | ||
this.form.value as { email: string; password: string } | ||
); | ||
this.loadingService.dismiss(); | ||
this.router.navigate(['/']); | ||
} catch (error) { | ||
// TODO proper handling of this error, give the Users an explanation/way to resolve | ||
this.loadingService.dismiss(); | ||
const alert = await this.alert.create({ | ||
message: 'Failed to register. Try again with a different email.', | ||
buttons: ['OK'], | ||
}); | ||
alert.present(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(AuthService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { | ||
Auth, | ||
UserCredential, | ||
createUserWithEmailAndPassword, | ||
signInWithEmailAndPassword, | ||
signOut, | ||
} from '@angular/fire/auth'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthService { | ||
constructor(private auth: Auth) {} | ||
|
||
register(creds: { | ||
email: string; | ||
password: string; | ||
}): Promise<UserCredential> { | ||
// try { | ||
|
||
// } catch (error) { | ||
|
||
// } | ||
return createUserWithEmailAndPassword( | ||
this.auth, | ||
creds.email, | ||
creds.password | ||
); | ||
} | ||
|
||
login(creds: { email: string; password: string }): Promise<UserCredential> { | ||
return signInWithEmailAndPassword(this.auth, creds.email, creds.password); | ||
} | ||
|
||
logout(): Promise<void> { | ||
return signOut(this.auth); | ||
} | ||
} |