Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth interceptor #9

Merged
merged 3 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/_providers/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';

import { AuthService } from '../_services/auth';
import { Observable } from 'rxjs/Observable';


@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: AuthService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if( this.auth.isAuthenticated() ) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
}

return next.handle(request);
}
}
22 changes: 15 additions & 7 deletions src/_services/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { User } from '../_models/user';
Expand All @@ -13,16 +13,16 @@ export class AuthService{
private url: string = appConfig.apiUrl + '/auth/login';
private httpOptions = {headers: new HttpHeaders({ 'Content-Type': 'application/json' })};

constructor(private http: HttpClient) {
constructor(private http: HttpClient) {
}

/**
* Realiza o signin na aplicação. O retorno representa um login realizado ou não.
* @param email
* @param password
* @param email
* @param password
*/
signIn(email: string, password: string) : Observable<boolean>{

let body = { email: email, password: password };

return this.http.post<User>( this.url, body, this.httpOptions)
Expand All @@ -38,7 +38,7 @@ export class AuthService{
return false;
})
}

/**
* Retorna se existe ou não um usuário autorizado no local storage
*/
Expand All @@ -50,10 +50,18 @@ export class AuthService{
}
}

/**
* Retorna o token de autenticação.
* @returns {string}
*/
getToken() : string {
return this.token;
}

/**
* Realiza o sign out
*/
signOut() {
localStorage.removeItem('authUser');
}
}
}
8 changes: 4 additions & 4 deletions src/_services/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import { User } from '../_models/user';
Expand All @@ -16,7 +16,7 @@ export class UsersService {

/**
* Cria um novo usuário
* @param user
* @param user
*/
create(user: User): Observable<User> {

Expand All @@ -34,7 +34,7 @@ export class UsersService {

/**
* Retorna um único usuário pelo seu id
* @param id
* @param id
*/
getOne(id: string) : Observable<User> {
return this.http.get<User>(this.url + '/' + id, this.httpOptions)
Expand All @@ -57,4 +57,4 @@ export class UsersService {
});
}

}
}
9 changes: 7 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';

Expand All @@ -18,7 +19,7 @@ import { AllEventsTab } from '../pages/events/all-events/all-events';
import { MyEventsTab } from '../pages/events/my-events/my-events';
import { SearchPage } from '../pages/search/search';
import { SignInPage } from '../pages/signin/signin';
import { SignUpPage } from '../pages/signup/signup';
import { SignUpPage } from '../pages/signup/signup';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
Expand All @@ -27,6 +28,7 @@ import { MatchesService } from '../_services/matches';
import { EventsService } from '../_services/events';
import { UsersService } from '../_services/users';
import { SearchedProfilePage } from '../pages/searched-profile/searched-profile';
import {TokenInterceptor} from "../_providers/auth.interceptor";

@NgModule({
declarations: [
Expand Down Expand Up @@ -73,7 +75,10 @@ import { SearchedProfilePage } from '../pages/searched-profile/searched-profile'
AuthService,
MatchesService,
EventsService,
UsersService
UsersService,
{provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true}
]
})
export class AppModule {}