Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions api/controllers/user.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.' })
Expand DownExpand Up@@ -128,4 +131,4 @@ router.get('/health', (req, res) => {
return res.send("I'm healthy!");
});

module.exports = router;
module.exports = router;
22 changes: 11 additions & 11 deletions frontend/src/app/Auth/Login.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,12 +21,12 @@ const LoginPage: React.FC<LoginSpecs.Props> = ({ login, history }) => {
return (
<Formik
initialValues={{
username: '',
emailOrUsername: '',
password: ''
}}
onSubmit={(values, formikActions) => {
setAuthError(false);
login(values.username, values.password)
login(values.emailOrUsername, values.password)
.then(resp =>
app.setAuthToken(resp.authToken).then(() => history.push(INVENTORY))
)
Expand All@@ -36,7 +36,7 @@ const LoginPage: React.FC<LoginSpecs.Props> = ({ 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')
})}
>
Expand All@@ -50,14 +50,14 @@ const LoginPage: React.FC<LoginSpecs.Props> = ({ login, history }) => {
<Box>
<h1>Sign In</h1>
{authError && (
<Alert message="Invalid username or password. Please try again." type="error"/>
<Alert message="Invalid email/username or password. Please try again." type="error"/>
)}
<Input label="Username"
value={values.username}
error={wasSubmitted && !!errors.username}
errorMsg={errors.username}
autocomplete="username"
onChange={v => setFieldValue('username', v)}/>
<Input label="Email/Username"
value={values.emailOrUsername}
error={wasSubmitted && !!errors.emailOrUsername}
errorMsg={errors.emailOrUsername}
autocomplete="emailOrUsername"
onChange={v => setFieldValue('emailOrUsername', v)}/>

<Input label="Password"
value={values.password}
Expand DownExpand Up@@ -94,4 +94,4 @@ const LoginWithApi = withApi<LoginSpecs.ApiProps>(api => ({
login: api.UserService.login
}))(LoginPage);

export default LoginWithApi;
export default LoginWithApi;
4 changes: 2 additions & 2 deletions frontend/src/app/Auth/types.d.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,7 +7,7 @@ export declare module LoginSpecs {
}

export interface FormValues {
username: string;
emailOrUsername: string;
password: string;
}

Expand DownExpand Up@@ -52,4 +52,4 @@ export declare module ResetPasswordSpecs {
}

export type Props = ApiProps & RouteComponentProps<{ callbackId: string }>;
}
}
6 changes: 3 additions & 3 deletions frontend/src/lib/api/user.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,8 +19,8 @@ export default class User implements UserService {
.then((resp: AxiosResponse<UserInfo>) => 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<AuthToken>) => resp.data);
};

Expand All@@ -43,4 +43,4 @@ export default class User implements UserService {
return this.api.post(`${this.baseUrl}/reset-password`, { callbackId, password })
.then((resp: AxiosResponse<null>) => resp.data);
}
}
}
4 changes: 2 additions & 2 deletions frontend/src/types/api/user.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,7 +19,7 @@ export interface Status {
}

export interface Login {
(username: string, password: string): Promise<AuthToken>;
(emailOrUsername: string, password: string): Promise<AuthToken>;
}

export interface Register {
Expand All@@ -36,4 +36,4 @@ export interface RequestReset {

export interface ResetPassword {
(callbackId: string, password: string): Promise<null>;
}
}