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