mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-05 14:00:41 -05:00
committed by
GitHub
parent
335044fece
commit
9b6042e44d
@@ -2,10 +2,9 @@ import dedent from 'dedent';
|
||||
import { check } from 'express-validator';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import passport from 'passport';
|
||||
import fetch from 'node-fetch';
|
||||
import { isEmail } from 'validator';
|
||||
|
||||
import { jwtSecret } from '../../../../config/secrets';
|
||||
|
||||
import { decodeEmail } from '../../common/utils';
|
||||
import {
|
||||
createPassportCallbackAuthenticator,
|
||||
@@ -14,7 +13,11 @@ import {
|
||||
} from '../component-passport';
|
||||
import { wrapHandledError } from '../utils/create-handled-error.js';
|
||||
import { removeCookies } from '../utils/getSetAccessToken';
|
||||
import { ifUserRedirectTo, ifNoUserRedirectHome } from '../utils/middleware';
|
||||
import {
|
||||
ifUserRedirectTo,
|
||||
ifNoUserRedirectHome,
|
||||
ifNotMobileRedirect
|
||||
} from '../utils/middleware';
|
||||
import { getRedirectParams } from '../utils/redirection';
|
||||
import { createDeleteUserToken } from '../middlewares/user-token';
|
||||
|
||||
@@ -34,6 +37,7 @@ module.exports = function enableAuthentication(app) {
|
||||
// enable loopback access control authentication. see:
|
||||
// loopback.io/doc/en/lb2/Authentication-authorization-and-permissions.html
|
||||
app.enableAuth();
|
||||
const ifNotMobile = ifNotMobileRedirect();
|
||||
const ifUserRedirect = ifUserRedirectTo();
|
||||
const ifNoUserRedirect = ifNoUserRedirectHome();
|
||||
const devSaveAuthCookies = devSaveResponseAuthCookies();
|
||||
@@ -87,6 +91,8 @@ module.exports = function enableAuthentication(app) {
|
||||
createGetPasswordlessAuth(app)
|
||||
);
|
||||
|
||||
api.get('/mobile-login', ifNotMobile, ifUserRedirect, mobileLogin(app));
|
||||
|
||||
app.use(api);
|
||||
};
|
||||
|
||||
@@ -188,3 +194,53 @@ function createGetPasswordlessAuth(app) {
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
function mobileLogin(app) {
|
||||
const {
|
||||
models: { User }
|
||||
} = app;
|
||||
return async function getPasswordlessAuth(req, res, next) {
|
||||
try {
|
||||
const auth0Res = await fetch(
|
||||
`https://${process.env.AUTH0_DOMAIN}/userinfo`,
|
||||
{
|
||||
headers: { Authorization: req.headers.authorization }
|
||||
}
|
||||
);
|
||||
|
||||
if (!auth0Res.ok) {
|
||||
return next(
|
||||
wrapHandledError(new Error('Invalid Auth0 token'), {
|
||||
type: 'danger',
|
||||
message: 'We could not log you in, please try again in a moment.',
|
||||
status: auth0Res.status
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { email } = await auth0Res.json();
|
||||
|
||||
if (!isEmail(email)) {
|
||||
return next(
|
||||
wrapHandledError(new TypeError('decoded email is invalid'), {
|
||||
type: 'danger',
|
||||
message: 'The email is incorrectly formatted',
|
||||
status: 400
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
User.findOne$({ where: { email } })
|
||||
.do(async user => {
|
||||
if (!user) {
|
||||
user = await User.create({ email });
|
||||
}
|
||||
await user.mobileLoginByRequest(req, res);
|
||||
res.end();
|
||||
})
|
||||
.subscribe(() => {}, next);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user