-
Notifications
You must be signed in to change notification settings - Fork 0
Auth #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khalska
wants to merge
19
commits into
master
Choose a base branch
from
auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Auth #4
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
46cebeb
Add login page.
khalska 4c618b7
Fix linter errors.
khalska 52a6f1c
Get user data.
khalska 2bb37ad
Visibility depends on user.
khalska b2bf0e3
Adding layout component to able router links in header.
khalska 7a245e7
Getting user from API.
khalska 46dee0d
Check posts of actual user.
khalska 6d70709
Styling login page.
khalska ab338cf
Set links to login.
khalska e1d64af
Change requests to modified API
khalska f5c206f
Create AuthComponent to protect from unauthorised access.
khalska b3b275a
Fixes in updating and deleting post.
khalska a1c869d
Validation login form.
khalska 321d9db
Validation.
khalska 22286a6
Merge branch 'redux' into auth
khalska 43023b7
Add Layout.
khalska 1dc772a
Fixes to 4 PR
khalska 6ce2fa5
Save token in localStorage - user will be logged in after refresh page
khalska 0d3cbd8
Fixes linter errors
khalska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,121 @@ | ||
| import {config} from "../config"; | ||
|
|
||
| export function setIsLogged(bool) { | ||
|
|
||
| return { | ||
| type: 'SET_IS_LOGGED', | ||
| isLogged: bool | ||
| }; | ||
| } | ||
|
|
||
| export function setToken(token) { | ||
| return { | ||
| type: 'SET_TOKEN', | ||
| token | ||
| }; | ||
| } | ||
|
|
||
| export function setUserData(userData) { | ||
| return { | ||
| type: 'SET_USER_DATA', | ||
| userData | ||
| }; | ||
| } | ||
|
|
||
| export function signIn(login, password) { | ||
| return (dispatch) => { | ||
| dispatch(fetchSignIn(login,password)); | ||
| } | ||
| } | ||
|
|
||
| export function fetchSignIn(login, password) { | ||
| return (dispatch, getState) => { | ||
| const data = { | ||
| login, | ||
| password | ||
| } | ||
|
|
||
| const myParams = Object.keys(data).map((key) => { | ||
| return `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`; | ||
| }).join('&'); | ||
|
|
||
| const fetchData = { | ||
| method: 'POST', | ||
| body: myParams, | ||
| //credentials: 'include', | ||
| headers: { | ||
| //"Content-Type": "application/json" | ||
| 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', | ||
| 'Access-Control-Allow-Origin': 'http://localhost:3003' | ||
| } | ||
| } | ||
|
|
||
| const url = config.url.login; | ||
|
|
||
| fetch(url, fetchData) | ||
| .then((response) => { | ||
|
|
||
| if (!response.ok) { | ||
| throw Error(response.statusText); | ||
| } | ||
| return response; | ||
| }) | ||
| .then( (response) => response.json() ) | ||
| .then( (json) => { | ||
| dispatch(setToken(json.token)); | ||
| dispatch(setIsLogged(true)); | ||
| dispatch(fetchUserData(json.token)); | ||
| localStorage.setItem('reactAppToken', json.token); | ||
| }); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| export function checkIfLogged() { | ||
| return (dispatch) => { | ||
| const token = localStorage.getItem('reactAppToken'); | ||
|
|
||
| if (token !== 'null') { | ||
| dispatch(setIsLogged(true)); | ||
| dispatch(setToken(token)); | ||
| dispatch(fetchUserData(token)) | ||
| } else { | ||
| dispatch(setIsLogged(false)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function fetchUserData(token) { | ||
| return (dispatch) => { | ||
| const url = config.url.auth; | ||
|
|
||
| const fetchData = { | ||
| method: 'GET', | ||
| headers: { | ||
| authorization: token, | ||
| }, | ||
| } | ||
|
|
||
| fetch(url, fetchData) | ||
| .then((response) => { | ||
| if (!response.ok) { | ||
|
|
||
| throw Error(response.statusText); | ||
| } | ||
| return response; | ||
| }) | ||
| .then((response) => response.json()) | ||
| .then((json) => { | ||
| dispatch(setUserData(json)) | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| export function logOut() { | ||
| return (dispatch) => { | ||
| localStorage.removeItem('reactAppToken'); | ||
| dispatch(setToken('')); | ||
| dispatch(setIsLogged(false)); | ||
| dispatch(setUserData({})); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,38 @@ | ||
| import { config } from "../config"; | ||
| import { | ||
|
|
||
| } from './actions.js'; | ||
|
|
||
| export function setLoginFormValidation(formLoginIsValid, error) { | ||
| return { | ||
| type: 'SET_LOGIN_FORM_VALID', | ||
| formLoginIsValid | ||
| } | ||
| } | ||
|
|
||
| export function validateLoginForm(login, password) { | ||
| return (dispatch) => { | ||
| const errors = []; | ||
| let result = true; | ||
|
|
||
| const loginIsValid = validateInputText(login); | ||
| if (!loginIsValid) { | ||
| result = false; | ||
| errors.push(config.messages.empty_login); | ||
| } | ||
|
|
||
| const passwordIsValid = validateInputText(password); | ||
| if (!passwordIsValid) { | ||
| dispatch(setLoginFormValidation(passwordIsValid)); | ||
| result = false; | ||
| errors.push(config.messages.empty_password); | ||
| } | ||
| //dispatch(setInfo(errors)); | ||
| dispatch(setLoginFormValidation(true)); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| export function validateInputText(value) { | ||
| return !(value.length === 0); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,42 @@ | ||
| import React from 'react'; | ||
| import { connect } from 'react-redux'; | ||
| import { browserHistory } from 'react-router'; | ||
| import PropTypes from 'prop-types'; | ||
| import { checkIfLogged } from '../../actions/auth'; | ||
|
|
||
| export function requireAuthentication(Component) { | ||
|
|
||
| class AuthComponent extends React.Component { | ||
| static propTypes = { | ||
| isLogged: PropTypes.bool.isRequired, | ||
| checkIfLogged: PropTypes.func.isRequired | ||
| } | ||
|
|
||
| componentWillMount() { | ||
| this.props.checkIfLogged(); | ||
| if (!this.props.isLogged) { | ||
| browserHistory.push('/login'); | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| this.props.isLogged && <Component {...this.props} /> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const mapStateToProps = | ||
| (state) => ({ | ||
| token: state.token, | ||
| isLogged: state.isLogged | ||
| }); | ||
|
|
||
| const mapDispatchToProps = (dispatch) => { | ||
| return { | ||
| checkIfLogged: () => dispatch(checkIfLogged()) | ||
| }; | ||
| } | ||
|
|
||
| return connect(mapStateToProps, mapDispatchToProps)(AuthComponent); | ||
| } |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if I refresh browser window. Will I be still logged in?