Skip to content

Commit

Permalink
feat(user): added user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Sep 5, 2021
1 parent c8fa6ff commit ccc19cc
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 45 deletions.
20 changes: 19 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ async function getUser(req, res) {
}
}

async function updateProfile(req, res) {
try {
const { name, avatar, title, work, education, skills, location, phone, socialMedia, websites } = req.body;

await Users.findOneAndUpdate({ _id: req.params.id }, {
name, avatar, title, work, education, skills, location, phone, socialMedia, websites
})

res.json({ msg: 'Updated profile' })
} catch (err) {

logger.error(err);

return res.status(500).json({ msg: err.message });
}
}

const createAccessToken = (user) => {
return jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1d' })
}
Expand All @@ -102,5 +119,6 @@ export {
register,
refreshToken,
login,
getUser
getUser,
updateProfile
};
30 changes: 30 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ const userSchema = new mongoose.Schema({
required: true,
trim: true
},
avatar: {
type: Object,
required: true,
default: "https://i.imgur.com/JSD2auk.png",
},
title: {
type: String,
},
work : {
type: Array,
},
education : {
type: Array,
},
skills : {
type: Array,
default: []
},
phone : {
type: String
},
socailMedia : {
type: Array,
},
websites : {
type: Array,
},
location : {
type: String,
},
email: {
type: String,
required: true,
Expand Down
5 changes: 4 additions & 1 deletion routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
register,
refreshToken,
login,
getUser
getUser,
updateProfile
} from '../controllers/user.js';

const router = express.Router();
Expand All @@ -17,4 +18,6 @@ router.get('/refresh_token', refreshToken);

router.get('/info', auth, getUser);

router.post('/update', auth, updateProfile);

export default router;
30 changes: 25 additions & 5 deletions src/API/UserAPI.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
import { useState, useEffect } from "react";
import axios from "axios";

const initialState = {
"role": 0,
"name": "",
"email": "",
"password": "",
"avatar": {},
"title" : "",
"location" : "",
"work" : [],
"education" : [],
"skills" : [],
"phone" : "",
"socialMedia" : [],
"websites" : [],
}

function UserAPI(token) {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [isAdmin, setIsAdmin] = useState(false);
const [history, setHistory] = useState([]);
const [user, setUser] = useState(initialState);
const [authenticated, isAuthenticated] = useState(false);

useEffect(() => {
if (token) {
const getUser = async () => {
try {
const res = await axios.get("/api/user/infor", {
headers: { Authorization: token },
try {
const res = await axios.get("/api/user/info", {
headers: { Authorization: token },
});
setIsLoggedIn(true);
isAuthenticated(true);
setUser(res.data);
res.data.role === 1 ? setIsAdmin(true) : setIsAdmin(false);

} catch (err) {
Expand All @@ -24,12 +44,12 @@ function UserAPI(token) {
}
}, [token]);



return {
isLoggedIn: [isLoggedIn, setIsLoggedIn],
isAdmin: [isAdmin, setIsAdmin],
history: [history, setHistory],
user: [user, setUser],
authenticated: [authenticated, isAuthenticated],
};
}

Expand Down
47 changes: 10 additions & 37 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,26 @@ import ArticleItem from './Pages/Articles/Article/Article';
import ProjectItem from './Pages/Projects/Project/Project';
import Login from './Pages/Auth/login';
import Register from './Pages/Auth/register';

import Profile from './Pages/User/profile';
import Editprofile from './Pages/User/editProfile';

export default class App extends Component {
state = {
user: null,
authenticated: false
}

render() {

return (

<BrowserRouter>
<Switch>
<Route
path="/"
exact
render={() => (
<Home
/>
)}
/>
<Route
path="/about"
exact
render={() => (
<About

/>
)}
/>
<Route
path="/shop"
exact
render={() => (
<Shop
/>
)}
/>
<Route path="/client" exact render={() => (
<Client
/>
)}
/>
<Route path="/project" exact component={Projects}/>
<Route path="/project/:id" exact component={ProjectItem}/>
<DataProvider>
<Route path="/" exact render={() => (<Home/>)}/>
<Route path="/about" exact render={() => ( <About/>)}/>
<Route path="/shop" exact render={() => (<Shop/>)}/>
<Route path="/profile" exact component={Profile}/>
<Route path="/edit" exact component={Editprofile}/>
<Route path="/client" exact component={Client}/>
<Route path="/project" exact component={Projects}/>
<Route path="/project/:id" exact component={ProjectItem}/>
<Route path="/login" exact component={Login} />
<Route path="/register" exact component={Register} />
<Route path="/blog" exact component={Articles} />
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const GlobalState = createContext()
export const DataProvider = ({ children }) => {
const [token, setToken] = useState(false)


useEffect(() => {
const firstLogin = localStorage.getItem("firstLogin");
if (firstLogin) {
const refreshToken = async () => {
const res = await axios.get("api/user/refresh_token");
setToken(res.data.accesstoken);

setTimeout(() => {
refreshToken();
}, 10 * 60 * 1000);
Expand Down

0 comments on commit ccc19cc

Please sign in to comment.