Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Router } from "angular2/router";
- import { status, json, fetch } from "../utils/fetch";
- import User from "./user";
- var instance = null;
- export default class Session {
- router: Router;
- currentUser: User;
- constructor(router: Router) {
- instance = this;
- this.router = router;
- }
- login({ login, password }: { login: string, password: string }) {
- console.log(`Logging as ${login} with password ${password}`);
- fetch("user/" + login, {
- method: "GET",
- headers: {
- "Accept": "application/json",
- "Content-Type": "application/json",
- "Authorization": "Basic " + btoa(login + ":" + password)
- }
- })
- .then(status)
- .then(json)
- .then(function(response) {
- console.log("LOGIN OK", response);
- this.authenticated = true;
- this.currentUser = new User(response);
- // and then we redirect the user
- this.router.navigate(this.getHomeRoute());
- }.bind(this))
- .catch((error) => {
- console.error(error);
- });
- }
- logout() {
- this.authenticated = false;
- this.currentUser = null;
- this.router.navigate([ "Login" ]);
- }
- getHomeRoute() {
- switch (this.currentUser.roles[ 0 ]) {
- case "MOA":
- return [ "DashboardMOA" ];
- case "EXPL":
- return [ "DashboardExploitant" ];
- default:
- return [ "Hello" ];
- }
- }
- }
- Session.hasRole = function(role: string) {
- return instance && instance.currentUser && instance.currentUser.hasRole(role);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement