forked from Dev-47/quiz-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Dev-47#49 from struckchure/main
adds redux and connected sign in endpoint
- Loading branch information
Showing
23 changed files
with
303 additions
and
126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,11 +1,43 @@ | ||
import AuthLayout from "../../layouts/AuthLayout"; | ||
import InputGroup from "../../components/auth/InputGroup"; | ||
import React, { useState } from "react"; | ||
import { connect } from "react-redux"; | ||
|
||
import { loginUser } from "../../store/actions/auth"; | ||
|
||
function SignIn({ loginUser }) { | ||
const [username, set_username] = useState(""); | ||
const [password, set_password] = useState(""); | ||
|
||
const __login_user = (username, password) => { | ||
loginUser({ username, password }); | ||
}; | ||
|
||
export default function SignIn() { | ||
return ( | ||
<AuthLayout pgtitle="Login"> | ||
<InputGroup type="text" contenttitle="Username" /> | ||
<InputGroup type="password" contenttitle="Password" /> | ||
</AuthLayout> | ||
<div> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
__login_user(username, password); | ||
}} | ||
> | ||
<input | ||
type="text" | ||
value={username} | ||
onChange={(e) => { | ||
set_username(e.target.value); | ||
}} | ||
/> | ||
<input | ||
type="password" | ||
value={password} | ||
onChange={(e) => { | ||
set_password(e.target.value); | ||
}} | ||
/> | ||
|
||
<button>Login</button> | ||
</form> | ||
</div> | ||
); | ||
} | ||
|
||
export default connect(null, { loginUser })(SignIn); |
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,23 @@ | ||
import { AUTH_LOGIN } from "../types"; | ||
import { api } from "../utils"; | ||
|
||
export const loginUser = (user_creds) => async (dispatch) => { | ||
await api({ | ||
url: "/accounts/login/", | ||
method: "POST", | ||
data: user_creds, | ||
}) | ||
.then((response) => { | ||
dispatch({ | ||
type: AUTH_LOGIN, | ||
payload: response.data, | ||
}); | ||
}) | ||
.catch((err) => { | ||
// dispatch({ | ||
// action: SET_ERROR, | ||
// payload: err.response.data | ||
// }) | ||
console.log(err); | ||
}); | ||
}; |
File renamed without changes.
File renamed without changes.
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,16 @@ | ||
import { createStore, applyMiddleware } from "redux"; | ||
import { composeWithDevTools } from "redux-devtools-extension"; | ||
import thunk from "redux-thunk"; | ||
|
||
import reducers from "./reducers"; | ||
|
||
const initialState = {}; | ||
const middleware = [thunk]; | ||
|
||
const store = createStore( | ||
reducers, | ||
initialState, | ||
composeWithDevTools(applyMiddleware(...middleware)) | ||
); | ||
|
||
export default store; |
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,21 @@ | ||
import { AUTH_LOGIN } from "../types"; | ||
import { Storage } from "../utils"; | ||
|
||
const storage = new Storage(); | ||
|
||
const initialState = { | ||
is_authenticated: JSON.parse(storage.get("token"))?.access ? true : false, | ||
}; | ||
|
||
export default (state = initialState, action) => { | ||
switch (action.type) { | ||
case AUTH_LOGIN: | ||
storage.set("token", JSON.stringify(action.payload)); | ||
return { | ||
...state, | ||
is_authenticated: true, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
11 changes: 6 additions & 5 deletions
11
web/src/context/reducers/common.js → web/src/store/reducers/common.js
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,11 @@ | ||
import { combineReducers } from "redux"; | ||
|
||
import auth from "./auth"; | ||
import common from "./common"; | ||
|
||
const rootReducer = combineReducers({ | ||
auth, | ||
common, | ||
}); | ||
|
||
export default rootReducer; |
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,5 @@ | ||
export const AUTH_LOGIN = "AUTH_LOGIN"; | ||
export const AUTH_REGISTER = "AUTH_REGISTER"; | ||
export const AUTH_LOGOUT = "AUTH_LOGOUT"; | ||
|
||
export const SET_LOADING = "SET_LOADING"; |
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,51 @@ | ||
import axios from "axios"; | ||
|
||
export const combineReducers = | ||
(...reducers) => | ||
(state, action) => { | ||
for (let i = 0; i < reducers.length; i++) { | ||
state = reducers[i](state, action); | ||
} | ||
|
||
return state; | ||
}; | ||
|
||
export function getCookie(name) { | ||
let cookieValue = null; | ||
if (document.cookie && document.cookie !== "") { | ||
const cookies = document.cookie.split(";"); | ||
for (let i = 0; i < cookies.length; i++) { | ||
const cookie = cookies[i].trim(); | ||
// Does this cookie string begin with the name we want? | ||
if (cookie.substring(0, name.length + 1) === name + "=") { | ||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
break; | ||
} | ||
} | ||
} | ||
return cookieValue; | ||
} | ||
|
||
// axios settings | ||
|
||
var API_URL = "https://dev-47-quiz-app-api.herokuapp.com/api/v1/"; | ||
|
||
axios.defaults.baseURL = API_URL; | ||
axios.defaults.xsrfHeaderName = "X-CSRFToken"; | ||
axios.defaults.responseEncoding = "utf8"; | ||
|
||
export const api = axios.create({}); | ||
|
||
export class Storage { | ||
set(key, value) { | ||
localStorage.setItem(key, value); | ||
} | ||
|
||
get(key) { | ||
return localStorage.getItem(key); | ||
} | ||
|
||
remove(key) { | ||
localStorage.removeItem(key); | ||
} | ||
} |
Oops, something went wrong.