diff --git a/api/controllers/user.js b/api/controllers/user.js index 8a57fb4..b953d93 100644 --- a/api/controllers/user.js +++ b/api/controllers/user.js @@ -29,8 +29,11 @@ router.post('/register', (req, res) => { // Login router.post('/login', (req, res) => { - const { username, password } = req.body; - models.User.findOne({ where: { username } }) + const { emailOrUsername, password } = req.body; + models.User.findOne({ where: { [Op.or]: [ + { email: emailOrUsername }, + { username: emailOrUsername } + ] }}) .then(async user => { if (!user) { return res.status(401).json({ error: 'Invalid credentials.' }) @@ -128,4 +131,4 @@ router.get('/health', (req, res) => { return res.send("I'm healthy!"); }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/frontend/src/app/Auth/Login.tsx b/frontend/src/app/Auth/Login.tsx index 42541da..39debc1 100644 --- a/frontend/src/app/Auth/Login.tsx +++ b/frontend/src/app/Auth/Login.tsx @@ -21,12 +21,12 @@ const LoginPage: React.FC = ({ login, history }) => { return ( { setAuthError(false); - login(values.username, values.password) + login(values.emailOrUsername, values.password) .then(resp => app.setAuthToken(resp.authToken).then(() => history.push(INVENTORY)) ) @@ -36,7 +36,7 @@ const LoginPage: React.FC = ({ login, history }) => { }); }} validationSchema={Yup.object().shape({ - username: Yup.string().required('Username is required'), + emailOrUsername: Yup.string().required('Email or Username is required'), password: Yup.string().required('Password is required') })} > @@ -50,14 +50,14 @@ const LoginPage: React.FC = ({ login, history }) => {

Sign In

{authError && ( - + )} - setFieldValue('username', v)}/> + setFieldValue('emailOrUsername', v)}/> (api => ({ login: api.UserService.login }))(LoginPage); -export default LoginWithApi; \ No newline at end of file +export default LoginWithApi; diff --git a/frontend/src/app/Auth/types.d.ts b/frontend/src/app/Auth/types.d.ts index 7742e8a..a60702d 100644 --- a/frontend/src/app/Auth/types.d.ts +++ b/frontend/src/app/Auth/types.d.ts @@ -7,7 +7,7 @@ export declare module LoginSpecs { } export interface FormValues { - username: string; + emailOrUsername: string; password: string; } @@ -52,4 +52,4 @@ export declare module ResetPasswordSpecs { } export type Props = ApiProps & RouteComponentProps<{ callbackId: string }>; -} \ No newline at end of file +} diff --git a/frontend/src/lib/api/user.ts b/frontend/src/lib/api/user.ts index 1363966..6883fb1 100644 --- a/frontend/src/lib/api/user.ts +++ b/frontend/src/lib/api/user.ts @@ -19,8 +19,8 @@ export default class User implements UserService { .then((resp: AxiosResponse) => resp.data); }; - login: Login = (username: string, password: string) => { - return this.api.post(`${this.baseUrl}/login`, { username, password }) + login: Login = (emailOrUsername: string, password: string) => { + return this.api.post(`${this.baseUrl}/login`, { emailOrUsername, password }) .then((resp: AxiosResponse) => resp.data); }; @@ -43,4 +43,4 @@ export default class User implements UserService { return this.api.post(`${this.baseUrl}/reset-password`, { callbackId, password }) .then((resp: AxiosResponse) => resp.data); } -} \ No newline at end of file +} diff --git a/frontend/src/types/api/user.ts b/frontend/src/types/api/user.ts index c2cd3fc..ea735dc 100644 --- a/frontend/src/types/api/user.ts +++ b/frontend/src/types/api/user.ts @@ -19,7 +19,7 @@ export interface Status { } export interface Login { - (username: string, password: string): Promise; + (emailOrUsername: string, password: string): Promise; } export interface Register { @@ -36,4 +36,4 @@ export interface RequestReset { export interface ResetPassword { (callbackId: string, password: string): Promise; -} \ No newline at end of file +}