aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/main.js')
-rw-r--r--frontend/src/main.js39
1 files changed, 32 insertions, 7 deletions
diff --git a/frontend/src/main.js b/frontend/src/main.js
index 79ed47e..3deeda3 100644
--- a/frontend/src/main.js
+++ b/frontend/src/main.js
@@ -3,10 +3,11 @@ import axios from 'axios';
import $ from 'jquery';
import cookie from 'js-cookie';
-const LOGIN_API_URL = 'http://localhost:4000';
-const POSTGREST_API_URL = 'http://localhost:3000';
+const LOGIN_API_URL = process.env.LOGIN_API_URL;
+const POSTGREST_API_URL = process.env.POSTGREST_API_URL;
+const GOOGLE_OAUTH_ID = process.env.GOOGLE_OAUTH_ID;
-$('a#logout').click(function(event) {
+$('button#logout').click(function(event) {
event.preventDefault();
cookie.remove('jwt');
alert('Logged out.');
@@ -29,8 +30,8 @@ $('button#fetchTodosBtn').click(async function() {
});
}
} catch (err) {
+ console.error(err);
$('div#todos').html(String(err));
- alert(err);
}
});
@@ -39,13 +40,37 @@ $('form#loginForm').submit(async function(event) {
const username = $(this).find('input#username').val();
const password = $(this).find('input#password').val();
try {
- const resp = await axios.post(`${LOGIN_API_URL}/login`, { username, password });
+ const resp = await axios.post(`${LOGIN_API_URL}/login/userpass`, { username, password });
const payload = resp.data;
- const jwt = payload.jwt;
+ const { jwt, displayName } = payload;
cookie.set('jwt', jwt);
- alert(`Logged in as ${username}.`);
+ alert(`Logged in as ${displayName}.`);
} catch (err) {
cookie.remove('jwt');
alert(err);
}
});
+
+$('button#google-login').click(async function() {
+ const GoogleAuth = gapi.auth2.getAuthInstance();
+ try {
+ const user = await GoogleAuth.signIn();
+ const authResponse = user.getAuthResponse(true);
+ const resp = await axios.post(`${LOGIN_API_URL}/login/google`, { accessToken: authResponse.access_token });
+ const payload = resp.data;
+ const { jwt, displayName } = payload;
+ cookie.set('jwt', jwt);
+ alert(`Logged in as ${displayName}.`);
+ } catch (err) {
+ console.error(err);
+ }
+});
+
+window.gapiInit = () => {
+ console.log('gapiInit');
+ gapi.load('auth2', function() {
+ gapi.auth2.init({
+ client_id: GOOGLE_OAUTH_ID,
+ });
+ });
+};