Hi all, I need a little help as I’m trying to achieve a role-based authentication and unable to figure it out. I’m confused on how will I post the username and password to the backend i.e a php api and get the status and if the status gives me role==0 then the login will go to the admin otherwise if the role==1 it will navigate to the user page.
Below is the code I’m using:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Headers, Http} from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = 'http://yourdomain.com/PHP-Slim-Restful/api/';
export interface User {
name: string;
role: number;
}
/*
Generated class for the AuthProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class AuthProvider {
currentUser: User;
constructor(public http: Http) { }
login(name: string, pw: string) : Promise<boolean> {
return new Promise((resolve, reject) => {
if (name === 'admin' && pw === 'admin') {
this.currentUser = {
name: name,
role: 0
};
resolve(true);
} else if (name === 'user' && pw === 'user') {
this.currentUser = {
name: name,
role: 1
};
resolve(true);
} else {
resolve(false);
}
});
}
isLoggedIn() {
return this.currentUser != null;
}
isAdmin() {
return this.currentUser.role === 0;
}
logout() {
this.currentUser = null;
}
}