Skip to content

Commit

Permalink
starting auth logic
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-carmo committed Apr 12, 2024
1 parent d5ab14c commit 13ec4f3
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 48 deletions.
1 change: 1 addition & 0 deletions android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation project(':capacitor-clipboard')
implementation project(':capacitor-haptics')
implementation project(':capacitor-keyboard')
implementation project(':capacitor-preferences')
implementation project(':capacitor-share')
implementation project(':capacitor-status-bar')

Expand Down
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ project(':capacitor-haptics').projectDir = new File('../node_modules/@capacitor/
include ':capacitor-keyboard'
project(':capacitor-keyboard').projectDir = new File('../node_modules/@capacitor/keyboard/android')

include ':capacitor-preferences'
project(':capacitor-preferences').projectDir = new File('../node_modules/@capacitor/preferences/android')

include ':capacitor-share'
project(':capacitor-share').projectDir = new File('../node_modules/@capacitor/share/android')

Expand Down
Binary file modified news.apk
Binary file not shown.
3 changes: 1 addition & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
</ion-item>
</ion-menu-toggle>
<ion-menu-toggle>
<ion-item>
<ion-item (click)="signOut()">
<ion-label>Sair</ion-label>
<ion-button (click)="verifyToken()"></ion-button>
<ion-icon name="exit-sharp" slot="start"></ion-icon>
</ion-item>
</ion-menu-toggle>
Expand Down
8 changes: 0 additions & 8 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,4 @@ export class AppComponent {
async signOut() {
await this.auth.signOut();
}

async getUser() {
console.log(await this.auth.getUser())
}

async verifyToken(){
await this.auth.verifyToken()
}
}
1 change: 1 addition & 0 deletions src/app/login/login.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>Login</h1>
<ion-input fill="outline" shape="round" label="Senha:" labelPlacement="floating" name="password" ngModel type="password" required [(ngModel)]="password"></ion-input>

<div>
<ion-button fill="clear" shape="round">Criar Conta</ion-button>
<ion-button fill="clear" shape="round">Esqueceu a senha?</ion-button>
<ion-button fill="clear" shape="round" (click)="googleAuth()">
<ion-icon name="logo-google"></ion-icon>
Expand Down
16 changes: 8 additions & 8 deletions src/app/login/login.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class LoginPage {
private messageTimeout: any;

constructor(
private auth: AuthService,
public auth: AuthService,
private loadingControler: LoadingController,
private router: Router
) {}
Expand All @@ -30,30 +30,30 @@ export class LoginPage {
}

async emailAuth(email: string, password: string) {
this.showLoading();
await this.showLoading();
const error = await this.auth.emailSignIn(email, password);
this.dimisLoading();
await this.dimisLoading();

error ? this.errorMessage(error) : this.router.navigate(['/']);
}

async googleAuth() {
this.showLoading();
await this.showLoading();
const error = await this.auth.googleSignIn();
this.dimisLoading();
await this.dimisLoading();

error ? this.errorMessage(error) : this.router.navigate(['/']);
}

async showLoading() {
private async showLoading() {
this.loading = await this.loadingControler.create({
message: 'Enviando...',
});

this.loading.present();
await this.loading.present();
}

async dimisLoading() {
private async dimisLoading() {
if (this.loading) {
await this.loading.dismiss();
}
Expand Down
13 changes: 9 additions & 4 deletions src/app/services/auth-guard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import { AuthService } from './auth.service';
providedIn: 'root',
})
export class AuthGuard implements CanActivate {

constructor(private auth: AuthService, private router: Router) {}

async canActivate() {
const user = await this.auth.getUser();
try {
const result = await this.auth.isLogged();
if (!result) {
this.router.navigate(['/login']);
return false;
}

if (!user) {
return true;
} catch (error) {
this.router.navigate(['/login']);
return false;
}

return true;
}
}
51 changes: 25 additions & 26 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ export class AuthService {

async createUser(email: string, password: string) {
try {
const data = await this.auth.createUserWithEmailAndPassword(
await this.auth.createUserWithEmailAndPassword(
email,
password
);
await this.setToken(data);

return;
} catch (error) {
Expand All @@ -27,8 +26,8 @@ export class AuthService {

async emailSignIn(email: string, password: string) {
try {
const data = await this.auth.signInWithEmailAndPassword(email, password);
await this.setToken(data);
await this.auth.signInWithEmailAndPassword(email, password);

return;
} catch (error) {
return firebaseError(error);
Expand All @@ -38,8 +37,7 @@ export class AuthService {
async googleSignIn() {
try {
const provider = new firebase.auth.GoogleAuthProvider();
const data = await this.auth.signInWithPopup(provider);
await this.setToken(data);
await this.auth.signInWithPopup(provider);

return;
} catch (error) {
Expand Down Expand Up @@ -83,19 +81,21 @@ export class AuthService {
}
}

async verifyToken() {
try {
const token = await this.getToken();

if (!token) {
return this.router.navigate(['/login']);
}

const data = await this.auth.signInWithCustomToken(token);
return await this.setToken(data);
} catch (error) {
return firebaseError(error);
}
async isLogged(): Promise<any> {
return new Promise((resolve, reject) => {
this.auth.authState.subscribe(
(user) => {
if (user) {
resolve(user);
} else {
reject('Usuário não logado');
}
},
(error) => {
reject(error);
}
);
});
}

async getUser() {
Expand All @@ -107,22 +107,21 @@ export class AuthService {
}
}

async getToken() {
async getToken(key: string) {
try {
const token = await Preferences.get({ key: 'token' });
const token = await Preferences.get({ key });
return token.value;
} catch (error) {
return firebaseError(error);
return 'Erro interno do servidor'
}
}

async setToken(data: any) {
async setToken(key: string, value: any) {
try {
const token = await data.user?.getIdToken();
if (token) await Preferences.set({ key: 'token', value: token });
await Preferences.set({ key, value });
return;
} catch (error) {
return firebaseError(error);
return 'Erro interno do servidor'
}
}
}

0 comments on commit 13ec4f3

Please sign in to comment.