]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 1 | 'use strict'; |
2 | ||
989fb5c7 | 3 | import cookies from './cookies.js'; |
4 | import page from 'page'; | |
5 | import React from 'react'; | |
6 | import ReactDOM from 'react-dom'; | |
87b6d05e | 7 | import {Header, Footer} from './header_footer.js'; |
7a9e5112 | 8 | |
9 | var App = {}; | |
b94f3416 | 10 | var cookieExpire = 60 * 60 * 24 * 7; |
7a9e5112 | 11 | |
12 | App.errorCodeToMessage = function(code) { | |
13 | switch (code) { | |
14 | case 'invalid_email': | |
15 | return 'The email is not valid'; | |
16 | case 'invalid_password': | |
17 | return 'The password is not valid'; | |
18 | case 'email_exists': | |
19 | return 'This email is already registered'; | |
20 | case 'invalid_credentials': | |
21 | return 'Invalid credentials'; | |
22 | case 'invalid_otp': | |
23 | return 'Invalid code !'; | |
24 | case 'user_not_confirmed': | |
25 | return 'Your account is being confirmed. Should be very soon !'; | |
26 | } | |
27 | ||
28 | return code; | |
29 | }; | |
30 | ||
31 | App.isUserSignedIn = function() { | |
32 | return cookies.hasItem('jwt'); | |
33 | }; | |
34 | ||
6bf174a9 | 35 | App.isUserAdmin = function() { |
36 | return cookies.hasItem('isAdmin') && cookies.getItem('isAdmin') === 'true'; | |
37 | }; | |
38 | ||
7a9e5112 | 39 | App.getUserToken = function() { |
40 | return cookies.getItem('jwt'); | |
41 | }; | |
42 | ||
6bf174a9 | 43 | App.onUserSignIn = function(token, isAdmin) { |
7a9e5112 | 44 | if (!token || token === '') { |
45 | page('/signin'); | |
46 | return; | |
47 | } | |
48 | ||
49 | cookies.setItem('jwt', token, cookieExpire); | |
6bf174a9 | 50 | cookies.setItem('isAdmin', isAdmin, cookieExpire); |
7a9e5112 | 51 | page('/me'); |
52 | }; | |
53 | ||
54 | App.onUserValidateOtp = function(token) { | |
55 | if (!token || token === '') { | |
56 | page('/signin'); | |
57 | return; | |
58 | } | |
59 | ||
60 | cookies.setItem('jwt', token, cookieExpire); | |
61 | page('/me'); | |
62 | }; | |
63 | ||
6bf174a9 | 64 | App.onUserSignUp = function(token, isAdmin) { |
7a9e5112 | 65 | if (!token || token === '') { |
66 | page('/signin'); | |
67 | return; | |
68 | } | |
69 | ||
70 | cookies.setItem('jwt', token, cookieExpire); | |
6bf174a9 | 71 | cookies.setItem('isAdmin', isAdmin, cookieExpire); |
adf936f6 | 72 | page('/not_confirmed'); |
7a9e5112 | 73 | }; |
74 | ||
75 | App.getUserJWT = function() { | |
76 | return cookies.getItem('jwt'); | |
77 | }; | |
78 | ||
79 | App.page = function(path, needsAuth, fn) { | |
80 | page(path, function(context) { | |
81 | if (needsAuth && !App.isUserSignedIn()) { | |
82 | page('/signin'); | |
83 | return; | |
84 | } | |
85 | ||
86 | fn(context); | |
87 | }); | |
88 | }; | |
89 | ||
90 | App.go = function(path) { | |
91 | page(path); | |
92 | }; | |
93 | ||
94 | App.start = function() { | |
95 | page(); | |
96 | }; | |
97 | ||
98 | App.onInternNavigation = function(href, e) { | |
99 | e.preventDefault(); | |
100 | page(href); | |
101 | }; | |
102 | ||
103 | App.onUserNotAuthorized = function(httpCode, apiCode) { | |
104 | switch (apiCode) { | |
105 | case 'not_authorized': | |
106 | cookies.removeItem('jwt'); | |
6bf174a9 | 107 | cookies.removeItem('isAdmin'); |
7a9e5112 | 108 | page('/signin'); |
109 | return false; | |
110 | case 'otp_not_setup': | |
111 | page('/otp/setup'); | |
112 | return false; | |
113 | case 'need_otp_validation': | |
114 | page('/otp/validate'); | |
115 | return false; | |
adf936f6 | 116 | case 'user_not_confirmed': |
117 | page('/not_confirmed'); | |
118 | return false; | |
7a9e5112 | 119 | default: |
120 | return true; | |
121 | } | |
122 | }; | |
123 | ||
124 | App.mount = function(app) { | |
125 | var root = React.createElement( | |
126 | 'div', | |
127 | {className: 'container'}, | |
87b6d05e | 128 | [ |
6bf174a9 | 129 | React.createElement(Header, {key: 'header', isLoggedIn: App.isUserSignedIn(), isAdmin: App.isUserAdmin()}), |
87b6d05e | 130 | React.createElement(React.Fragment, {key: 'app'}, app), |
6bf174a9 | 131 | React.createElement(Footer, {key: 'footer', isLoggedIn: App.isUserSignedIn(), isAdmin: App.isUserAdmin()}) |
87b6d05e | 132 | ], |
7a9e5112 | 133 | ); |
134 | ||
135 | ReactDOM.unmountComponentAtNode(document.getElementById('app')); | |
136 | ReactDOM.render(root, document.getElementById('app')); | |
137 | }; | |
138 | ||
989fb5c7 | 139 | export default App; |