-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
export class User{ | ||
_id: string; | ||
name: string; | ||
email: string; | ||
username: string; | ||
token: string; | ||
|
||
constructor(_id: string, name: string, email: string){ | ||
this._id = _id; | ||
this.name = name; | ||
this.email = email; | ||
constructor(_id: string, username: string, email: string){ | ||
this.username = username; | ||
this.token = ""; | ||
} | ||
} |
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,45 +1,61 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import 'rxjs/add/operator/map' | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Observable } from 'rxjs/observable'; | ||
import 'rxjs/add/operator/map'; | ||
|
||
import { User } from '../_models/user'; | ||
import { appConfig } from '../app/app.config'; | ||
|
||
@Injectable() | ||
export class AuthService{ | ||
constructor(private http: HttpClient) { } | ||
|
||
signIn(email: string, password: string) { | ||
return this.http.post<any>(appConfig.apiUrl + '/authenticate', { email: email, password: password }) | ||
.map(user => { | ||
// login successful if there's a jwt token in the response | ||
if (user && user.token) { | ||
if(user.success){ | ||
localStorage.setItem('currentUser', JSON.stringify(user)); | ||
} | ||
} | ||
console.log(user); | ||
return user; | ||
}); | ||
public token: string; | ||
private url: string = appConfig.apiUrl + '/api/auth/login'; | ||
private httpOptions = {headers: new HttpHeaders({ 'Content-Type': 'application/json' })}; | ||
|
||
constructor(private http: HttpClient) { | ||
let authUser = JSON.parse(localStorage.getItem('authUser')); | ||
this.token = authUser && authUser.token; | ||
} | ||
|
||
fakeAuth(user: string, pass: string){ | ||
let fakeUser = { | ||
name: user, | ||
password: pass | ||
} | ||
localStorage.setItem('currentUser', JSON.stringify(fakeUser)); | ||
/** | ||
* Realiza o signin na aplicação. O retorno representa um login realizado ou não. | ||
* @param username | ||
* @param password | ||
*/ | ||
signIn(username: string, password: string) : Observable<boolean>{ | ||
|
||
let body = { username: username, password: password }; | ||
|
||
return this.http.post<User>( this.url, body, this.httpOptions) | ||
.map( user => { | ||
let token = user && user.token; | ||
if (token) { | ||
this.token = token; | ||
localStorage.setItem('authUser', JSON.stringify(user)); | ||
//Signin realizado | ||
return true; | ||
} | ||
//Signin não realizado | ||
return false; | ||
}) | ||
} | ||
|
||
isAuthenticated(){ | ||
if(localStorage.getItem('currentUser')){ | ||
/** | ||
* Retorna se existe ou não um usuário autorizado no local storage | ||
*/ | ||
isAuthenticated() : boolean{ | ||
if(localStorage.getItem('authUser')){ | ||
return true | ||
}else{ | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Realiza o sign out | ||
*/ | ||
signOut() { | ||
// remove user from local storage to log user out | ||
localStorage.removeItem('currentUser'); | ||
localStorage.removeItem('authUser'); | ||
} | ||
} |
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,3 +1,3 @@ | ||
export const appConfig = { | ||
apiUrl: 'http://localhost:3000' | ||
apiUrl: 'http://10.0.0.9:4040' | ||
}; |
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