-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added auth form. Added login and logout actions
- Loading branch information
1 parent
f5ed854
commit e4690d5
Showing
11 changed files
with
199 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
client/src/components/Settings/AppDetails/AppDetails.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
.AppVersion { | ||
.text { | ||
color: var(--color-primary); | ||
margin-bottom: 15px; | ||
} | ||
|
||
.AppVersion a { | ||
.text a, | ||
.text span { | ||
color: var(--color-accent); | ||
} | ||
} | ||
|
||
.separator { | ||
margin: 30px 0; | ||
border: 1px solid var(--color-primary); | ||
} |
113 changes: 90 additions & 23 deletions
113
client/src/components/Settings/AppDetails/AppDetails.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,100 @@ | ||
import { Fragment } from 'react'; | ||
import { FormEvent, Fragment, useState } from 'react'; | ||
|
||
// Redux | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
import { actionCreators } from '../../../store'; | ||
import { State } from '../../../store/reducers'; | ||
|
||
// UI | ||
import { Button, InputGroup, SettingsHeadline } from '../../UI'; | ||
|
||
// CSS | ||
import classes from './AppDetails.module.css'; | ||
import { Button } from '../../UI'; | ||
|
||
// Utils | ||
import { checkVersion } from '../../../utility'; | ||
|
||
export const AppDetails = (): JSX.Element => { | ||
const { isAuthenticated } = useSelector((state: State) => state.auth); | ||
|
||
const dispatch = useDispatch(); | ||
const { login, logout } = bindActionCreators(actionCreators, dispatch); | ||
|
||
const [password, setPassword] = useState(''); | ||
|
||
const formHandler = (e: FormEvent) => { | ||
e.preventDefault(); | ||
login(password); | ||
setPassword(''); | ||
}; | ||
|
||
return ( | ||
<Fragment> | ||
<p className={classes.AppVersion}> | ||
<a | ||
href="https://github.com/pawelmalak/flame" | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
Flame | ||
</a>{' '} | ||
version {process.env.REACT_APP_VERSION} | ||
</p> | ||
<p className={classes.AppVersion}> | ||
See changelog{' '} | ||
<a | ||
href="https://github.com/pawelmalak/flame/blob/master/CHANGELOG.md" | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
here | ||
</a> | ||
</p> | ||
<Button click={() => checkVersion(true)}>Check for updates</Button> | ||
<SettingsHeadline text="Authentication" /> | ||
{!isAuthenticated ? ( | ||
<form onSubmit={formHandler}> | ||
<InputGroup> | ||
<label htmlFor="password">Password</label> | ||
<input | ||
type="password" | ||
id="password" | ||
name="password" | ||
placeholder="••••••" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
/> | ||
<span> | ||
See | ||
<a | ||
href="https://github.com/pawelmalak/flame/wiki/Authentication" | ||
target="blank" | ||
> | ||
{` project wiki `} | ||
</a> | ||
to read more about authentication | ||
</span> | ||
</InputGroup> | ||
|
||
<Button>Login</Button> | ||
</form> | ||
) : ( | ||
<div> | ||
<p className={classes.text}> | ||
You are logged in. Your session will expire <span>@@@@</span> | ||
</p> | ||
<Button click={logout}>Logout</Button> | ||
</div> | ||
)} | ||
|
||
<hr className={classes.separator} /> | ||
|
||
<div> | ||
<SettingsHeadline text="App version" /> | ||
<p className={classes.text}> | ||
<a | ||
href="https://github.com/pawelmalak/flame" | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
Flame | ||
</a>{' '} | ||
version {process.env.REACT_APP_VERSION} | ||
</p> | ||
|
||
<p className={classes.text}> | ||
See changelog{' '} | ||
<a | ||
href="https://github.com/pawelmalak/flame/blob/master/CHANGELOG.md" | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
here | ||
</a> | ||
</p> | ||
|
||
<Button click={() => checkVersion(true)}>Check for updates</Button> | ||
</div> | ||
</Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Dispatch } from 'redux'; | ||
import { ApiResponse } from '../../interfaces'; | ||
import { ActionType } from '../action-types'; | ||
import { LoginAction, LogoutAction } from '../actions/auth'; | ||
import axios, { AxiosError } from 'axios'; | ||
|
||
export const login = | ||
(password: string) => async (dispatch: Dispatch<LoginAction>) => { | ||
try { | ||
const res = await axios.post<ApiResponse<{ token: string }>>( | ||
'/api/auth', | ||
{ password } | ||
); | ||
|
||
localStorage.setItem('token', res.data.data.token); | ||
|
||
dispatch({ | ||
type: ActionType.login, | ||
payload: res.data.data.token, | ||
}); | ||
} catch (err) { | ||
const apiError = err as AxiosError; | ||
|
||
dispatch<any>({ | ||
type: ActionType.createNotification, | ||
payload: { | ||
title: 'Error', | ||
message: apiError.response?.data.error, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
export const logout = () => (dispatch: Dispatch<LogoutAction>) => { | ||
localStorage.removeItem('token'); | ||
|
||
dispatch({ | ||
type: ActionType.logout, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ActionType } from '../action-types'; | ||
|
||
export interface LoginAction { | ||
type: ActionType.login; | ||
payload: string; | ||
} | ||
|
||
export interface LogoutAction { | ||
type: ActionType.logout; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Action } from '../actions'; | ||
import { ActionType } from '../action-types'; | ||
|
||
interface AuthState { | ||
isAuthenticated: boolean; | ||
token: string | null; | ||
} | ||
|
||
const initialState: AuthState = { | ||
isAuthenticated: false, | ||
token: null, | ||
}; | ||
|
||
export const authReducer = ( | ||
state: AuthState = initialState, | ||
action: Action | ||
): AuthState => { | ||
switch (action.type) { | ||
case ActionType.login: | ||
return { | ||
...state, | ||
token: action.payload, | ||
isAuthenticated: true, | ||
}; | ||
case ActionType.logout: | ||
return { | ||
...state, | ||
token: null, | ||
isAuthenticated: false, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters