Skip to content

Commit

Permalink
Getting the user information from the backend to use in other endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
berkaydoner committed Dec 14, 2021
1 parent 7ce6335 commit 6ee7919
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
21 changes: 20 additions & 1 deletion frontend/src/Controllers/AuthInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ export const setHeaders = (accessToken, refreshToken) =>{
headers.refresh = refreshToken
headers.accessTimeStamp = new Date().getTime()
}

export const setUserInfo = (username,user_id) => {
headers.username = username
headers.user_id = user_id
}
export const getUserInfoLoggedIn = ()=>{
if(headers.username!==null) return ({username:headers.username,user_id:headers.user_id})
return false
}
export const getUserInfo = async (username) =>{
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}
return fetch("/api/users/"+username+"/",options)
.then(response=>response.json())
.then(r=>{
console.log(r);
setUserInfo(username, r.id);
return r})
}
export const getToken = async _=>{
if(headers.accessTimeStamp === null) return false
if(new Date().getTime()-headers.accessTimeStamp < 1000*60*5) return headers.access
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/Controllers/CommentAnswerController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as CommentAnswerController from "./CommentAnswerController"
import {getUserInfoLoggedIn} from "./AuthInfo";

export async function getAnswersOfComment(post_id,comment_id){
const options = {
Expand Down Expand Up @@ -73,7 +74,9 @@ export async function getCommentsAndAnswersOfEvent(post_id){
}
return comments
}
export function postComment(post_id,owner_id,username,content){
export function postComment(post_id,content){
let username = getUserInfoLoggedIn().username
let owner_id = getUserInfoLoggedIn().user_id
const date = new Date().toISOString()
const options = {
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
Expand Down Expand Up @@ -108,7 +111,9 @@ export function postComment(post_id,owner_id,username,content){
console.log("comment",comment)
return comment
}
export function postAnswer(post_id,comment_id,owner_id,username,content){
export function postAnswer(post_id,comment_id,content){
let username = getUserInfoLoggedIn().username
let owner_id = getUserInfoLoggedIn().user_id
const date = new Date().toISOString()
const options = {
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/Controllers/EventController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {getToken} from "./AuthInfo";
import {getToken, getUserInfoLoggedIn} from "./AuthInfo";

export function postEvent(params){
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json' , "Authorization": getToken()},
method: 'POST',
body: JSON.stringify({
owner: params.owner,
owner: getUserInfoLoggedIn().user_id,
content: params.content?params.content:"",
title: params.title,
location: params.location,
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/Views/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Link, useNavigate} from 'react-router-dom'
import {obtainToken} from "../../Controllers/LoginController";
import {useState} from "react";
import {Alert} from "@mui/lab";
import {setHeaders, getToken} from "../../Controllers/AuthInfo";
import {setHeaders, getToken, getUserInfo} from "../../Controllers/AuthInfo";

const initialState = {
username: {
Expand Down Expand Up @@ -51,6 +51,7 @@ export default function LoginPage(props){
}
else{
setHeaders(r.refresh, r.access)
getUserInfo(state.username).then(r => console.log(r))
navigate("/")
}
})
Expand Down

0 comments on commit 6ee7919

Please sign in to comment.