/** * Handles a signed in user. Sets the session cookie and then redirects to * profile page on success. * @param {!firebase.User} user */ const handleSignedInUser = function(user) { // Show redirection notice. document.getElementById('redirecting').classList.remove('hidden'); // Set session cookie user.getIdToken().then(function(idToken) { // Session login endpoint is queried and the session cookie is set. // CSRF token should be sent along with request. const csrfToken = getCookie('csrfToken') return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken) .then(function() { // Redirect to profile on success. window.location.assign('/profile'); }, function(error) { // Refresh page on error. // In all cases, client side state should be lost due to in-memory // persistence. window.location.assign('/'); }); }); }
// FETCH BATCH FROM API async function getBatch() { let authToken = await firebase.auth().currentUser.getIdToken(false); let data = await request .get('/api/getBatch') .set('token', authToken) // .query({ token: authToken }) let response; try { response = JSON.parse(data.text); console.log('mnist data: ', response); } catch(error) { response = error; response.success = false; } return response; }
setContext(async (_, { headers }) => { let token; if (firebase.apps.length && firebase.auth().currentUser) { token = await firebase .auth() .currentUser.getIdToken(true); } return { headers: { ...headers, authorization: token ? `Bearer ${token}` : '', 'Accept-Language': getLanguageCode(), }, }; })
firebase .auth() .signInWithEmailAndPassword(user.email, user.password) .then((data) => { return data.user.getIdToken(); }) .then((token) => { return res.json({ token }); }) .catch((err) => { console.error(err); // auth/wrong-password // auth/user-not-user return res .status(403) .json({ general: 'Wrong credentials, please try again' }); });
// SUBMIT GUESS async function submitGuess(guess, mnist_id) { let authToken = await firebase.auth().currentUser.getIdToken(false); let data = await request .post('/api/verifyImage') .set('token', authToken) .send({ guess: guess, mnist_id: mnist_id }) let response; try { response = JSON.parse(data.text); console.log('post response: ', response); } catch(error) { response = error; response.success = false; } return response; }
const token = await user.getIdToken(true) console.log('auth:', token) user.token = token
/** * Handles a signed in user. Sets the session cookie and then redirects to * profile page on success. * @param {!firebase.User} user */ const handleSignedInUser = function(user) { // Show redirection notice. document.getElementById('redirecting').classList.remove('hidden'); // Set session cookie user.getIdToken().then(function(idToken) { // Session login endpoint is queried and the session cookie is set. // CSRF token should be sent along with request. const csrfToken = getCookie('csrfToken') return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken) .then(function() { // Redirect to profile on success. window.location.assign('/profile'); }, function(error) { // Refresh page on error. // In all cases, client side state should be lost due to in-memory // persistence. window.location.assign('/'); }); }); }
const token = await user.getIdToken(true) console.log('auth:', token) user.token = token