aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjloup <jloup@jloup.work>2018-05-13 23:07:26 +0200
committerjloup <jloup@jloup.work>2018-05-13 23:07:26 +0200
commit6bf174a95ba0f71abf25397316fc101405381cdf (patch)
tree4a7915347ea102b10701a0c1e04655d7d95f5df8
parentd5b8f0ffbbde5fb8d41c3abb4b0969b962746b52 (diff)
downloadFront-6bf174a95ba0f71abf25397316fc101405381cdf.tar.gz
Front-6bf174a95ba0f71abf25397316fc101405381cdf.tar.zst
Front-6bf174a95ba0f71abf25397316fc101405381cdf.zip
Admin user token in frontend.
-rw-r--r--cmd/web/js/app.js15
-rw-r--r--cmd/web/js/header_footer.jsx14
-rw-r--r--cmd/web/js/signin.jsx2
-rw-r--r--cmd/web/js/signup.jsx2
4 files changed, 27 insertions, 6 deletions
diff --git a/cmd/web/js/app.js b/cmd/web/js/app.js
index 878878d..4756057 100644
--- a/cmd/web/js/app.js
+++ b/cmd/web/js/app.js
@@ -32,17 +32,22 @@ App.isUserSignedIn = function() {
32 return cookies.hasItem('jwt'); 32 return cookies.hasItem('jwt');
33}; 33};
34 34
35App.isUserAdmin = function() {
36 return cookies.hasItem('isAdmin') && cookies.getItem('isAdmin') === 'true';
37};
38
35App.getUserToken = function() { 39App.getUserToken = function() {
36 return cookies.getItem('jwt'); 40 return cookies.getItem('jwt');
37}; 41};
38 42
39App.onUserSignIn = function(token) { 43App.onUserSignIn = function(token, isAdmin) {
40 if (!token || token === '') { 44 if (!token || token === '') {
41 page('/signin'); 45 page('/signin');
42 return; 46 return;
43 } 47 }
44 48
45 cookies.setItem('jwt', token, cookieExpire); 49 cookies.setItem('jwt', token, cookieExpire);
50 cookies.setItem('isAdmin', isAdmin, cookieExpire);
46 page('/me'); 51 page('/me');
47}; 52};
48 53
@@ -56,13 +61,14 @@ App.onUserValidateOtp = function(token) {
56 page('/me'); 61 page('/me');
57}; 62};
58 63
59App.onUserSignUp = function(token) { 64App.onUserSignUp = function(token, isAdmin) {
60 if (!token || token === '') { 65 if (!token || token === '') {
61 page('/signin'); 66 page('/signin');
62 return; 67 return;
63 } 68 }
64 69
65 cookies.setItem('jwt', token, cookieExpire); 70 cookies.setItem('jwt', token, cookieExpire);
71 cookies.setItem('isAdmin', isAdmin, cookieExpire);
66 page('/not_confirmed'); 72 page('/not_confirmed');
67}; 73};
68 74
@@ -98,6 +104,7 @@ App.onUserNotAuthorized = function(httpCode, apiCode) {
98 switch (apiCode) { 104 switch (apiCode) {
99 case 'not_authorized': 105 case 'not_authorized':
100 cookies.removeItem('jwt'); 106 cookies.removeItem('jwt');
107 cookies.removeItem('isAdmin');
101 page('/signin'); 108 page('/signin');
102 return false; 109 return false;
103 case 'otp_not_setup': 110 case 'otp_not_setup':
@@ -119,9 +126,9 @@ App.mount = function(app) {
119 'div', 126 'div',
120 {className: 'container'}, 127 {className: 'container'},
121 [ 128 [
122 React.createElement(Header, {key: 'header', isLoggedIn: App.isUserSignedIn()}), 129 React.createElement(Header, {key: 'header', isLoggedIn: App.isUserSignedIn(), isAdmin: App.isUserAdmin()}),
123 React.createElement(React.Fragment, {key: 'app'}, app), 130 React.createElement(React.Fragment, {key: 'app'}, app),
124 React.createElement(Footer, {key: 'footer', isLoggedIn: App.isUserSignedIn()}) 131 React.createElement(Footer, {key: 'footer', isLoggedIn: App.isUserSignedIn(), isAdmin: App.isUserAdmin()})
125 ], 132 ],
126 ); 133 );
127 134
diff --git a/cmd/web/js/header_footer.jsx b/cmd/web/js/header_footer.jsx
index f11ed06..3e87c1e 100644
--- a/cmd/web/js/header_footer.jsx
+++ b/cmd/web/js/header_footer.jsx
@@ -12,6 +12,12 @@ class MyAccount extends React.Component {
12 } 12 }
13} 13}
14 14
15class Admin extends React.Component {
16 render = () => {
17 return <a href="/admin"><u>Admin</u></a>;
18 }
19}
20
15class Logo extends React.Component { 21class Logo extends React.Component {
16 render() { 22 render() {
17 return <div id="logo" className="w-100 h-100 align-self-center"> 23 return <div id="logo" className="w-100 h-100 align-self-center">
@@ -27,6 +33,10 @@ class Footer extends React.Component {
27 elements = []; 33 elements = [];
28 } 34 }
29 35
36 if (this.props.isAdmin === true) {
37 elements = elements.concat(<Admin />);
38 }
39
30 if (this.props.isLoggedIn === true) { 40 if (this.props.isLoggedIn === true) {
31 elements = elements.concat(<MyAccount />); 41 elements = elements.concat(<MyAccount />);
32 elements = elements.concat(<Signout />); 42 elements = elements.concat(<Signout />);
@@ -58,6 +68,10 @@ class Header extends React.Component {
58 elements = []; 68 elements = [];
59 } 69 }
60 70
71 if (this.props.isAdmin === true) {
72 elements = elements.concat(<Admin />);
73 }
74
61 if (this.props.isLoggedIn === true) { 75 if (this.props.isLoggedIn === true) {
62 elements = elements.concat(<MyAccount />); 76 elements = elements.concat(<MyAccount />);
63 elements = elements.concat(<Signout />); 77 elements = elements.concat(<Signout />);
diff --git a/cmd/web/js/signin.jsx b/cmd/web/js/signin.jsx
index e0e0f1d..5b3b661 100644
--- a/cmd/web/js/signin.jsx
+++ b/cmd/web/js/signin.jsx
@@ -18,7 +18,7 @@ class SigninForm extends React.Component {
18 } 18 }
19 19
20 this.displayMessage('OK', true); 20 this.displayMessage('OK', true);
21 this.props.onSuccess(data.token); 21 this.props.onSuccess(data.token, data.isAdmin);
22 22
23 }.bind(this)); 23 }.bind(this));
24 e.preventDefault(); 24 e.preventDefault();
diff --git a/cmd/web/js/signup.jsx b/cmd/web/js/signup.jsx
index b7d9287..08509e8 100644
--- a/cmd/web/js/signup.jsx
+++ b/cmd/web/js/signup.jsx
@@ -24,7 +24,7 @@ class SignupForm extends React.Component {
24 } 24 }
25 25
26 this.displayMessage('Thank You. Your account is being confirmed. Check your mailbox soon', true); 26 this.displayMessage('Thank You. Your account is being confirmed. Check your mailbox soon', true);
27 this.props.onSuccess(data.token); 27 this.props.onSuccess(data.token, data.isAdmin);
28 28
29 }.bind(this) 29 }.bind(this)
30); 30);