-
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.
Dinamically change user in the entire app
- Loading branch information
1 parent
b058e71
commit 16a00bd
Showing
8 changed files
with
163 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,67 @@ | ||
import { Component, Input, OnInit } from "@angular/core"; | ||
import { ModalController } from "@ionic/angular"; | ||
import { UserApiService } from "src/app/api/apis/users.api"; | ||
|
||
import { UserDetail } from "./../../api/types/user-detail"; | ||
import { LoginComponent } from "./../../shared/login/login.component"; | ||
|
||
@Component({ | ||
selector: "app-menu", | ||
templateUrl: "./menu.component.html", | ||
styleUrls: ["./menu.component.scss"] | ||
styleUrls: ["./menu.component.scss"], | ||
}) | ||
export class MenuComponent implements OnInit { | ||
@Input() contentId: string; | ||
|
||
isAuthenticated: boolean = true; | ||
user = { | ||
username: "MyUser", | ||
email: "myUser@emai.com" | ||
}; | ||
isAuthenticated; | ||
user: UserDetail; | ||
appPages = [ | ||
{ | ||
url: "posts", | ||
icon: "smiley", | ||
title: "All Posts" | ||
title: "All Posts", | ||
}, | ||
{ | ||
url: "country", | ||
icon: "earth", | ||
title: "By country" | ||
title: "Maps", | ||
}, | ||
{ | ||
url: "about", | ||
icon: "about", | ||
title: "About" | ||
} | ||
title: "About", | ||
}, | ||
]; | ||
|
||
public categories = ["Family", "Friends", "Notes", "Work", "Travel", "Reminders"]; | ||
|
||
constructor() {} | ||
constructor(private userService: UserApiService, public modalController: ModalController) { | ||
this.user = this.userService.getLoggedInUser(); | ||
if (this.user.name === "guest") { | ||
this.isAuthenticated = false; | ||
} else { | ||
this.isAuthenticated = true; | ||
} | ||
} | ||
|
||
ngOnInit() {} | ||
|
||
presentModal() { | ||
const modal = this.modalController.create({ | ||
component: LoginComponent, | ||
swipeToClose: true, | ||
showBackdrop: true, | ||
}); | ||
modal.then((x) => { | ||
x.present(); | ||
x.onDidDismiss().then(() => { | ||
this.user = this.userService.getLoggedInUser(); | ||
if (this.user.name === "guest") { | ||
this.isAuthenticated = false; | ||
} else { | ||
this.isAuthenticated = true; | ||
} | ||
}); | ||
}); | ||
} | ||
} |
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,46 @@ | ||
<ion-content style="width: 100%; height: 100%;"> | ||
<ion-grid style="height: 100%;"> | ||
<ion-row style="height: 100%;" class="ion-justify-content-center ion-align-items-center"> | ||
<ion-col style="height: 90%;" sizeLg="12" sizeMd="12" sizeXs="12"> | ||
<ion-card style="height: 100%;"> | ||
<ion-card-header> | ||
<ion-card-title *ngIf="!toggle"> | ||
Login | ||
</ion-card-title> | ||
<ion-card-title *ngIf="toggle"> | ||
Sign-up | ||
</ion-card-title> | ||
</ion-card-header> | ||
<ion-card-content> | ||
<ion-item *ngIf="loggedIn === false"> | ||
<ion-text color="danger">Invalid username or password</ion-text> | ||
</ion-item> | ||
<ion-item> | ||
<ion-label>Login</ion-label> | ||
<ion-toggle [(ngModel)]="toggle"></ion-toggle> | ||
<ion-label class="ion-text-end">Sign Up</ion-label> | ||
</ion-item> | ||
|
||
<ion-item> | ||
<ion-input placeholder="Username" [(ngModel)]="username"></ion-input> | ||
</ion-item> | ||
<ion-item> | ||
<ion-input | ||
placeholder="Password" | ||
type="password" | ||
[(ngModel)]="password" | ||
></ion-input> | ||
</ion-item> | ||
<ion-button | ||
fill="solid" | ||
type="submit" | ||
(click)="login()" | ||
[disabled]="!username || !password" | ||
>Submit</ion-button | ||
> | ||
</ion-card-content> | ||
</ion-card> | ||
</ion-col> | ||
</ion-row> | ||
</ion-grid> | ||
</ion-content> |
Empty file.
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,33 @@ | ||
import { Component, OnInit } from "@angular/core"; | ||
import { ModalController } from "@ionic/angular"; | ||
import { UserApiService } from "src/app/api/apis/users.api"; | ||
|
||
@Component({ | ||
selector: "app-login", | ||
templateUrl: "./login.component.html", | ||
styleUrls: ["./login.component.scss"], | ||
}) | ||
export class LoginComponent implements OnInit { | ||
toggle = false; | ||
username; | ||
password; | ||
loggedIn; | ||
|
||
constructor(public modalCtrl: ModalController, private userService: UserApiService) {} | ||
|
||
ngOnInit() {} | ||
|
||
login() { | ||
if (!!this.username && !!this.password) { | ||
if (this.toggle) { | ||
this.userService.signUp(this.username, this.password); | ||
this.modalCtrl.dismiss(); | ||
} else { | ||
this.loggedIn = this.userService.logIn(this.username, this.password); | ||
if (this.loggedIn) { | ||
this.modalCtrl.dismiss(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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