front/auth: move auth state to API
This commit is contained in:
parent
851bbc4b60
commit
e95ed4cc0b
4 changed files with 38 additions and 41 deletions
|
@ -4,6 +4,11 @@ const request = require('superagent');
|
|||
const config = require('./config.js');
|
||||
|
||||
class Api {
|
||||
constructor() {
|
||||
this.userName = null;
|
||||
this.userPassword = null;
|
||||
}
|
||||
|
||||
get(url) {
|
||||
const fullUrl = this.getFullUrl(url);
|
||||
return this.process(fullUrl, () => request.get(fullUrl));
|
||||
|
@ -31,6 +36,25 @@ class Api {
|
|||
});
|
||||
}
|
||||
|
||||
hasPrivilege() {
|
||||
/* TODO: implement */
|
||||
return true;
|
||||
}
|
||||
|
||||
login(userName, userPassword) {
|
||||
this.userName = userName;
|
||||
this.userPassword = userPassword;
|
||||
}
|
||||
|
||||
logout() {
|
||||
this.userName = null;
|
||||
this.userPassword = null;
|
||||
}
|
||||
|
||||
isLoggedIn() {
|
||||
return this.userName !== null;
|
||||
}
|
||||
|
||||
getFullUrl(url) {
|
||||
return (config.basic.apiUrl + '/' + url).replace(/([^:])\/+/g, '$1/');
|
||||
}
|
||||
|
|
|
@ -8,42 +8,16 @@ class AuthController {
|
|||
this.api = api;
|
||||
this.topNavigationController = topNavigationController;
|
||||
this.loginView = loginView;
|
||||
this.currentUser = null;
|
||||
/* TODO: load from cookies */
|
||||
}
|
||||
|
||||
isLoggedIn() {
|
||||
return this.currentUser !== null;
|
||||
}
|
||||
|
||||
hasPrivilege() {
|
||||
return true;
|
||||
}
|
||||
|
||||
login(userName, userPassword) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.api.userName = userName;
|
||||
this.api.userPassword = userPassword;
|
||||
this.api.get('/user/' + userName)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
logout(user) {
|
||||
this.currentUser = null;
|
||||
this.api.userName = null;
|
||||
this.api.userPassword = null;
|
||||
/* TODO: clear cookie */
|
||||
}
|
||||
|
||||
loginRoute() {
|
||||
this.topNavigationController.activate('login');
|
||||
this.loginView.render({
|
||||
login: (userName, userPassword, doRemember) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this
|
||||
.login(userName, userPassword)
|
||||
this.api.login(userName, userPassword);
|
||||
this.api.get('/user/' + userName)
|
||||
.then(response => {
|
||||
if (doRemember) {
|
||||
/* TODO: set cookie */
|
||||
|
@ -59,6 +33,7 @@ class AuthController {
|
|||
|
||||
logoutRoute() {
|
||||
this.topNavigationController.activate('logout');
|
||||
/* TODO: clear cookie */
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ class NavigationItem {
|
|||
}
|
||||
|
||||
class TopNavigationController {
|
||||
constructor(topNavigationView, authController) {
|
||||
this.authController = authController;
|
||||
constructor(topNavigationView, api) {
|
||||
this.api = api;
|
||||
this.topNavigationView = topNavigationView;
|
||||
|
||||
this.items = {
|
||||
|
@ -37,22 +37,22 @@ class TopNavigationController {
|
|||
for (let key of b) {
|
||||
this.items[key].available = true;
|
||||
}
|
||||
if (!this.authController.hasPrivilege('posts:list')) {
|
||||
if (!this.api.hasPrivilege('posts:list')) {
|
||||
this.items.posts.available = false;
|
||||
}
|
||||
if (!this.authController.hasPrivilege('posts:upload')) {
|
||||
if (!this.api.hasPrivilege('posts:upload')) {
|
||||
this.items.upload.available = false;
|
||||
}
|
||||
if (!this.authController.hasPrivilege('comments:list')) {
|
||||
if (!this.api.hasPrivilege('comments:list')) {
|
||||
this.items.comments.available = false;
|
||||
}
|
||||
if (!this.authController.hasPrivilege('tags:list')) {
|
||||
if (!this.api.hasPrivilege('tags:list')) {
|
||||
this.items.tags.available = false;
|
||||
}
|
||||
if (!this.authController.hasPrivilege('users:list')) {
|
||||
if (!this.api.hasPrivilege('users:list')) {
|
||||
this.items.users.available = false;
|
||||
}
|
||||
if (this.authController.isLoggedIn()) {
|
||||
if (this.api.isLoggedIn()) {
|
||||
this.items.register.available = false;
|
||||
this.items.login.available = false;
|
||||
} else {
|
||||
|
|
|
@ -30,12 +30,10 @@ const helpView = new HelpView();
|
|||
const loginView = new LoginView();
|
||||
const registrationView = new RegistrationView();
|
||||
|
||||
const authController = new AuthController(api, null, loginView);
|
||||
const topNavigationController
|
||||
= new TopNavigationController(topNavigationView, authController);
|
||||
// break cyclic dependency topNavigationView<->authController
|
||||
authController.topNavigationController = topNavigationController;
|
||||
|
||||
= new TopNavigationController(topNavigationView, api);
|
||||
const authController = new AuthController(
|
||||
api, topNavigationController, loginView);
|
||||
const homeController = new HomeController(topNavigationController);
|
||||
const postsController = new PostsController(topNavigationController);
|
||||
const usersController = new UsersController(
|
||||
|
|
Loading…
Reference in a new issue