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