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