Skip to content
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

Redux state management for Authentication related things #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"react-uwp": "^1.3.1",
"redux": "^4.0.5"
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import reducer from './store/reducer';

const store = createStore(reducer);
const store = createStore(reducer, applyMiddleware(thunk));

ReactDOM.render(
<React.StrictMode>
Expand Down
55 changes: 22 additions & 33 deletions src/main/ForgotPassword.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import { NavLink } from "react-router-dom";
import axios from 'axios';

import { connect } from "react-redux";
import TextBox from "react-uwp/TextBox";
import AppBarButton from "react-uwp/AppBarButton";
import * as actionCreators from '../store/actions/actionCreators';


export default class ForgotPassword extends React.Component {
class ForgotPassword extends React.Component {

constructor(props) {
super(props);
this.state = { email: '', errMessage: '' };
this.state = { email: '' };
this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(event) {

const axiosOptions = {
'method': 'POST',
'url': `${process.env.REACT_APP_DB_HOST}/auth/reset/request`,
'data': {
'email': this.state.email,
}
}

axios(axiosOptions)
.then(response => {
this.setState({ errMessage: "Ah, Dementia. Reset mail sent!" });
})
.catch(error => {
let status = error.response.status

if (status === 401) {
this.setState({
errMessage: "User is not registered. Please sign up first.",
errHref: "/signup"
});
}
else {
this.setState({ errMessage: "It's not you, it's us. Try again later." })
}
})
this.props.submitForgotPass(this.state.email);
}

componentDidMount() {
Expand Down Expand Up @@ -111,7 +85,7 @@ export default class ForgotPassword extends React.Component {
/>
<br />
<span onClick={this.handleSubmit}>
<a href={this.state.errHref}><span>{this.state.errMessage}</span></a>
<a href={this.props.followLink}><span>{this.props.message}</span></a>
<AppBarButton
style={{ margin: "10px auto", ...buttonStyle }}
icon={<span className="sdl2asset">&#xF286;</span>}
Expand All @@ -124,4 +98,19 @@ export default class ForgotPassword extends React.Component {
</div>
);
}
}
}

const mapStateToProps = state => {
return {
message: state.forgotPassMessage,
followLink: state.forgotPassFollowLink
}
}

const mapDispatchToProps = dispatch => {
return {
submitForgotPass: email => dispatch(actionCreators.submitForgotPass(email))
}
}

export default connect(mapStateToProps, mapDispatchToProps)(ForgotPassword);
54 changes: 22 additions & 32 deletions src/main/ResendEmail.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import axios from 'axios'

import { NavLink } from "react-router-dom";
import { connect } from "react-redux";
import TextBox from "react-uwp/TextBox";
import AppBarButton from "react-uwp/AppBarButton";
import * as actionCreators from '../store/actions/actionCreators';


export default class ResendEmail extends React.Component {
class ResendEmail extends React.Component {

constructor(props) {
super(props);
this.state = { email: '', errMessage: '' };
this.state = { email: ''};
this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit(event) {

const axiosOptions = {
'method': 'POST',
'url': `${process.env.REACT_APP_DB_HOST}/auth/resendVerificationEmail`,
'data': {
'email': this.state.email,
}
}

axios(axiosOptions)
.then(response => {
this.setState({ errMessage: "Verification mail sent!" });
})
.catch(error => {
let status = error.response.status

if (status === 401) {
this.setState({
errMessage: "User is not registered. Please sign up first.",
errHref: "/signup"
});
}
else {
this.setState({ errMessage: "It's not you, it's us. Try again later." })
}
})
this.props.resendEmail(this.state.email);
}

componentDidMount() {
Expand Down Expand Up @@ -111,7 +86,7 @@ export default class ResendEmail extends React.Component {
/>
<br />
<span onClick={this.handleSubmit}>
<a href={this.state.errHref}><span>{this.state.errMessage}</span></a>
<a href={this.props.followLink}><span>{this.props.message}</span></a>
<AppBarButton
style={{ margin: "10px auto", ...buttonStyle }}
icon={<span className="sdl2asset">&#xE8FA;</span>}
Expand All @@ -124,4 +99,19 @@ export default class ResendEmail extends React.Component {
</div>
);
}
}
}

const mapStateToProps = state => {
return{
message : state.resendEmailMessage,
followLink: state.resendEmailFollowLink
}
}

const mapDispatchToProps = dispatch => {
return{
resendEmail : email => dispatch(actionCreators.resendEmail(email))
}
}

export default connect(mapStateToProps, mapDispatchToProps)(ResendEmail);
60 changes: 21 additions & 39 deletions src/main/SignIn.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import { NavLink, Link } from "react-router-dom";
import axios from 'axios';

import { connect } from "react-redux";
import TextBox from "react-uwp/TextBox";
import AppBarButton from "react-uwp/AppBarButton";
import PasswordBox from "react-uwp/PasswordBox";
import * as actionCreators from '../store/actions/actionCreators';

export default class SignIn extends React.Component {
class SignIn extends React.Component {

constructor(props) {
super(props);
this.state = { username: '', password: '', errMessage: '', errHref: '', remember: 'false' };
this.state = { username: '', password: '', remember: 'false' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
Expand All @@ -27,40 +27,7 @@ export default class SignIn extends React.Component {
}

handleSubmit(event) {

const axiosOptions = {
'method': 'POST',
'url': `${process.env.REACT_APP_DB_HOST}/auth/login`,
'data': {
'username': this.state.username,
'password': this.state.password,
'remember': this.state.remember
}
}

axios(axiosOptions)
.then(response => {
localStorage.setItem('access_token', response.data.access_token);
localStorage.setItem('refresh_token', response.data.refresh_token);
localStorage.setItem('userID', response.data.id);
localStorage.setItem('username', response.data.username);
window.location.pathname = "/profile";
})
.catch(error => {
let status = error.response.status;
if (status === 401) {
this.setState({ errMessage: "Couldn't verify. Please check credentials!" });
}

if (status === 402) {
this.setState({ errMessage: "Please verify your email before signing in. ", errHref: "/resendVerification" });
}

if (status === 403) {
this.setState({ errMessage: "Account not found. ", errHref: "/signup" });
}
})

this.props.signIn(this.state.username, this.state.password, this.state.remember);
}

componentDidMount() {
Expand Down Expand Up @@ -159,7 +126,7 @@ export default class SignIn extends React.Component {
<label for="signcheck">&nbsp; Keep me signed in</label>
<br />
<br />
<a href={this.state.errHref}><span>{this.state.errMessage}</span></a>
<a href={this.props.followLink}><span>{this.props.message}</span></a>
<span onClick={this.handleSubmit}>
<AppBarButton
style={{ margin: "10px auto", ...buttonStyle }}
Expand All @@ -176,3 +143,18 @@ export default class SignIn extends React.Component {
);
}
}

const mapStateToProps = state => {
return {
message: state.signInMessage,
followLink: state.signInFollowLink
}
}

const mapDispatchToProps = dispatch => {
return {
signIn: (username, password, remember) => dispatch(actionCreators.signIn(username, password, remember))
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignIn);
65 changes: 22 additions & 43 deletions src/main/SignUp.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as React from "react";
import * as PropTypes from "prop-types";
import axios from 'axios';

import { NavLink, Link } from "react-router-dom";
import { connect } from 'react-redux';
import TextBox from "react-uwp/TextBox";
import AppBarButton from "react-uwp/AppBarButton";
import PasswordBox from "react-uwp/PasswordBox";
import * as actionCreators from '../store/actions/actionCreators';


export default class SignUp extends React.Component {
class SignUp extends React.Component {

constructor(props) {
super(props);
this.state = { email: '', username: '', bio: '', password: '', confPassword: '', errMessage: '', errHref: '' };
this.state = { email: '', username: '', bio: '', password: '', confPassword: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
Expand All @@ -28,42 +27,7 @@ export default class SignUp extends React.Component {
}

handleSubmit(event) {

if (this.state.confPassword !== this.state.password) {
this.setState({ errMessage: "Passwords don't match" })
}
else {
const axiosOptions = {
'method': 'POST',
'url': `${process.env.REACT_APP_DB_HOST}/auth/signup`,
'data': {
'email': this.state.email,
'username': this.state.username,
'password': this.state.password,
'bio': this.state.bio
}
}

axios(axiosOptions)
.then(response => {
this.setState({ errMessage: "Signed Up! Please verify your email!" });
})
.catch(error => {
let status = error.response.status
//console.log(error.response);

if (status === 402) {
this.setState({ errMessage: "Username is taken" });
}

else if (status === 401) {
this.setState({ errMessage: "This email is registered with another account." });
}
else {
this.setState({ errMessage: "It's not you, it's us. Try again later." })
}
})
}
this.props.signUp(this.state.email, this.state.username, this.state.password, this.state.bio, this.state.confPassword);
}

componentDidMount() {
Expand Down Expand Up @@ -183,7 +147,7 @@ export default class SignUp extends React.Component {
<br />
<br />
<span onClick={this.handleSubmit}>
<a href={this.state.errHref}><span>{this.state.errMessage}</span></a>
<a href={this.props.followLink}><span>{this.props.message}</span></a>
<AppBarButton
style={{ margin: "10px auto", ...buttonStyle }}
icon={<span className="sdl2asset">&#xE8FA;</span>}
Expand All @@ -198,4 +162,19 @@ export default class SignUp extends React.Component {
</div>
);
}
}
}

const mapStateToProps = state => {
return {
message: state.signUpMessage,
followLink: state.signUpFollowLink
}
}

const mapDispatchToProps = dispatch => {
return {
signUp: (email, username, password, bio, confPassword) => dispatch(actionCreators.signUp(email, username, password, bio, confPassword))
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignUp);
Loading