Notes
Notes - notes.io |
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Router } from '@angular/router';
import { ProfileService } from './profile.service';
import { BaseResponse } from '../../http/response/response';
import { ChangePasswordRequest, RefreshTokenRequest, ResetPasswordRequest, UserLoginRequest, UserProfile, UserProfileData } from '../dictionary/auth-models';
import { API_URL } from '../../../config';
import jwt_decode from 'jwt-decode';
@Injectable({
providedIn: 'root'
})
export class AuthProvider {
constructor(private http: HttpClient,
private router: Router,
private profileService: ProfileService) {
}
// Method to login user
authenticate(data: UserLoginRequest): Observable<UserProfileData> {
return this.http.post<UserProfileData>(API_URL + 'Account/login', data).pipe(
map((res:UserProfileData) => {
if(!!res ) {
this.setTokenToLocalStorage(res.access_token);
this.setTokensObjectToLocalStorage(JSON.stringify(res));
const userDetailsObject = this.getDecodedAccessToken(res.access_token);
console.log(" userDetailsObject ",userDetailsObject);
if(userDetailsObject) {
const userProfile : UserProfile= {
id : userDetailsObject.id,
first_name : userDetailsObject.given_name,
last_name : userDetailsObject.family_name,
email : userDetailsObject.preferred_username,
access_token: res.access_token,
refreshToken: res.refreshToken
};
this.profileService.storeProfileData(userProfile);
this.profileService.updateProfileData(userProfile);
}
else {
this.logOutLocal();
}
}
else {
this.logOutLocal();
}
return res;
})
);
}
getDecodedAccessToken(token: string): any {
try{
return jwt_decode(token);
}
catch(Error){
return null;
}
}
// Method to register user
// registerCustomer(data: UserRegisterRequest): Observable<UserRegisterResponse> {
// return this.http.post<UserRegisterResponse>(API_URL + 'Customer/register', data).pipe(
// map((res:UserRegisterResponse) => {
// return res;
// })
// );
// }
// Method to check if customer exist
// existCustomer(data: UserExistRequest): Observable<UserRegisterResponse> {
// return this.http.post<UserRegisterResponse>(API_URL + 'Customer/CheckIfCustomerExists', data).pipe(
// map((res:UserRegisterResponse) => {
// return res;
// })
// );
// }
//method to verify customer
// verifyCustomer(verifyId:string): Observable<UserRegisterResponse> {
// return this.http.post<UserRegisterResponse>(API_URL + `Customer/verify?id=${verifyId}`, null).pipe(
// map((res:UserRegisterResponse) => {
// return res;
// })
// );
// }
// Method to logout user
logOut(): Observable<BaseResponse> {
return this.http.post<BaseResponse>(API_URL + 'Admin/logout', null).pipe(map((result:BaseResponse) => {
this.logOutLocal();
return result;
}));
}
// Method for forgot password
forgotPassword(data: ResetPasswordRequest): Observable<BaseResponse> {
return this.http.post<BaseResponse>(API_URL + 'Admin/PasswordReset', data).pipe(map((response:BaseResponse) => {
return response;
}));
}
// Method for reset password
resetPassword(data: ChangePasswordRequest): Observable<BaseResponse> {
return this.http.post<BaseResponse>(API_URL + 'Admin/ChangePassword', data);
}
// // Method for get link
// getLink(email: any, token: any): Observable<BaseResponse> {
// return this.http.get<BaseResponse>(API_URL + 'adminuser/password/reset/' + token + '/' + email);
// }
// Clearing Storages
logOutLocal() {
this.deleteTokenFromStorage();
this.profileService.removeProfileData();
this.profileService.updateProfileData(null);
this.router.navigateByUrl('/auth/login');
}
// method to refresh jwt token
refreshToken(): Observable<any | null> {
if(!!this.isAuthenticated()) {
const refreshTokenObj: RefreshTokenRequest = {
token : this.profileService?.getProfileData()?.refreshToken
};
return this.http.post<any>(`${API_URL}Account/RefreshToken/refresh-token`, refreshTokenObj)
.pipe(
map((res:any) => {
if(!!res) {
this.setTokenToLocalStorage(res.results.token);
this.profileService.storeProfileData(res?.results);
this.profileService.updateProfileData(res?.results);
}
else {
this.logOutLocal();
}
return res;
})
);
}
else {
return of(null);
}
}
// find is user logged in
public isAuthenticated(): boolean {
return !!this.getTokenFromLocalStorage();
}
/**
* Token Getters & Setters
*/
setTokenToLocalStorage(token: string) {
localStorage.setItem('token', token);
}
getTokenFromLocalStorage() {
return localStorage.getItem('token');
}
deleteTokenFromStorage() {
localStorage.removeItem('token');
}
setTokensObjectToLocalStorage(token: string) {
localStorage.setItem('tokenObj', token);
}
getTokensObjectFromLocalStorage() {
return localStorage.getItem('tokenObj');
}
deleteTokensObjectFromStorage() {
localStorage.removeItem('tokenObjs');
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
