aboutsummaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorRubén Beltrán del Río <ben@nsovocal.com>2017-01-30 02:47:14 -0600
committerGitHub <noreply@github.com>2017-01-30 02:47:14 -0600
commita6ccda0fbc4df683f9568d85eb22b21684d2a0bd (patch)
tree967e98f2d8bf6718858da1fbc279987208b25e06 /app/services
parent287fa13b3e600b2340895a5463a288bf08101bb5 (diff)
Create and Show Posts (#3)
* 📝 Update changelog (retroactive) SORRY * Add JSDoc * Document twitter helper * Add posts handler * Remove cookies, set routes under /api * Update readme for clearer callback instructions * Redirect callback to login with token * Add dasein paw helper * Remove unused hostname * Remove hostname, add expiration to config * Rename expiration ttl * Send TTL to posts handler * Add redis dependency * Add redis config to config file * Add DASEIN_REDIS_HOST to env.dist * Correct redis config structure * Add redis db to compose * Add redis to posts handler * Update dasein paw file * Git add uuid * Add first iteration of post handlers * Ignore docs in linter * Ignore generated assets * Ignore docs and assets * Add frontend dependencies + more koa * Add post creation to backend * Adjust readme to show actual callback route * Add npm build as part of docker process * Configure babel * Update paw * Add webpack config * Add frontend code * List globals
Diffstat (limited to 'app/services')
-rw-r--r--app/services/auth.js75
1 files changed, 75 insertions, 0 deletions
diff --git a/app/services/auth.js b/app/services/auth.js
new file mode 100644
index 0000000..32db321
--- /dev/null
+++ b/app/services/auth.js
@@ -0,0 +1,75 @@
+import Axios from 'axios';
+
+const internals = {};
+
+/* global localStorage */
+
+internals.kInitiateLoginRoute = '/api/auth/login';
+internals.kHandleCallbackRoute = '/api/auth/callback';
+
+export default internals.AuthService = class AuthService {
+
+ constructor() {
+
+ this.authenticated = false;
+
+ // Bootstrap from local storage.
+ if (localStorage.hasOwnProperty('user') && localStorage.hasOwnProperty('token')
+ && localStorage.hasOwnProperty('expiresAt')) {
+
+ if (parseInt(localStorage.getItem('expiresAt')) > Date.now()) {
+ this.expiresAt = localStorage.getItem('expiresAt');
+ this.user = JSON.parse(localStorage.getItem('user'));
+ this.token = localStorage.getItem('token');
+ this.authenticated = true;
+ }
+ }
+ }
+
+ // Initiates the login process. Resolves to an object that contains the URL
+ // to redirect to to continue processing.
+ initiateLogin() {
+
+ return Axios.get(internals.kInitiateLoginRoute).then((response) => {
+
+ return response.data;
+ });
+ }
+
+ // Posts the oAuthToken and verifier to the API to get back a user object
+ // and a signed JWT
+ getUserObject(oAuthToken, oAuthVerifier) {
+
+ return Axios.post(internals.kHandleCallbackRoute, {
+ oAuthToken,
+ oAuthVerifier
+ }).then((response) => {
+
+ return response.data;
+ });
+ }
+
+ // Logs in the user by setting the user and token
+ login(user, token, expiresAt) {
+
+ localStorage.setItem('user', JSON.stringify(user));
+ localStorage.setItem('token', token);
+ localStorage.setItem('expiresAt', expiresAt);
+ this.user = user;
+ this.token = token;
+ this.expiresAt = expiresAt;
+ this.authenticated = true;
+ }
+
+ // Logs a user out by removing the user and token
+ logout() {
+
+ localStorage.removeItem('user');
+ localStorage.removeItem('token');
+ localStorage.removeItem('expiresAt');
+ delete this.user;
+ delete this.token;
+ delete this.expiresAt;
+ this.authenticated = false;
+ }
+};