Skip to content

Instantly share code, notes, and snippets.

@navarroaxel
Created February 2, 2017 14:28
Show Gist options
  • Save navarroaxel/6a4edd160176f903e5a101b04421f38a to your computer and use it in GitHub Desktop.
Save navarroaxel/6a4edd160176f903e5a101b04421f38a to your computer and use it in GitHub Desktop.
/*@ngInject*/
export default function pingService($http, endpoint) {
return {
ping: () => $http.get(`${endpoint}ping`).then(response => response.data)
};
}
import sha256 from 'jssha/src/sha256';
/*@ngInject*/
export default function sessionService($rootScope, $http, $q, $window, localStorageService, endpoint, pingService) {
const USER_SESSION = 'userSession',
USERS = 'users',
CREDENTIALS = 'credentials';
let idSurveyLastVisited = null;
function setSession(user) {
$rootScope.isLoggedIn = !!user;
let auth = null;
if (user) {
auth = `Basic ${$window.btoa(user._id + ':' + user.password)}`;
} else {
idSurveyLastVisited = null;
}
$http.defaults.headers.common.Authorization = auth;
}
function loginLocally(username, password) {
let users = localStorageService.get(USERS);
if (!users) {
return;
}
let user = users[username];
if (!user) {
return;
}
if (user.password != sha256(password)) {
return null;
}
localStorageService.set(USER_SESSION, user);
setSession(user);
return user;
}
function loginOnServer(username, password) {
return $http.post(`${endpoint}sign-in`, {username: username, password: password}).then(
response => {
let users = localStorageService.get(USERS);
users = users || {};
users[response.data._id] = response.data;
localStorageService.set(USERS, users);
localStorageService.set(USER_SESSION, response.data);
setSession(response.data);
return response.data;
},
response => {
if (response.status == 403) {
localStorageService.remove(username);
}
return response.error;
});
}
setSession(localStorageService.get(USER_SESSION));
return {
getCurrent: () => localStorageService.get(USER_SESSION),
login: (username, password) => pingService.ping().then(
() => loginOnServer(username, password),
() => loginLocally(username, password)
),
logout: () => {
let user = localStorageService.get(USER_SESSION);
localStorageService.set(CREDENTIALS, {username: user._id});
localStorageService.remove(USER_SESSION);
setSession();
},
getLastCredentials: () => localStorageService.get(CREDENTIALS),
setLastVisited: idSurvey => idSurveyLastVisited = idSurvey,
getLastVisited: () => idSurveyLastVisited
};
}
/*@ngInject*/
export default class SignInController {
constructor($window, $state, sessionService, surveyService, messagesService) {
this.$window = $window;
this.$state = $state;
this.sessionService = sessionService;
this.surveyService = surveyService;
this.messagesService = messagesService;
this.login = () => {
return sessionService.login(this.username, this.password).then(
user => {
this.working = false;
if (user) {
this.$state.go('survey.areas');
return true;
}
this.invalidLogin = true;
return false;
},
() => {
this.invalidLogin = true;
this.working = false;
}
);
};
}
$onInit() {
let user = this.sessionService.getCurrent();
if (user) {
return this.$state.go('survey.areas');
}
let credentials = this.sessionService.getLastCredentials();
if (credentials) {
this.lastUser = this.username = credentials.username;
this.$window.document.getElementById('password').focus();
} else {
this.$window.document.getElementById('username').focus();
}
}
signIn() {
this.working = true;
this.errorMessage = '';
if (!this.username && !this.password) {
return;
}
if (this.lastUser && this.lastUser != this.username) {
return this.changeUserWarning = true;
}
this.invalidLogin = false;
this.login().then(
loggedIn => {
if (loggedIn) {
this.messagesService.fetchMessages();
}
}
);
}
continueChangeUser() {
this.login().then(
loggedIn => {
if (loggedIn) {
this.surveyService.clear();
this.messagesService.clear().then(
() => this.messagesService.fetchMessages()
);
}
}
);
}
cancelChangeUser() {
this.username = this.lastUser;
this.password = null;
this.changeUserWarning = false;
this.$window.document.getElementById('password').focus();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment