Skip to content

Commit a581a41

Browse files
Login screen account creation
1 parent 9e6e36a commit a581a41

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

Backend/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ app.get('/registerAccount', async (req, res) => {
193193
}
194194

195195
const hashedPassword = await bcrypt.hash(password, 10);
196-
db.insertOne({username: username, password: hashedPassword, canvasUser: null, lastLogin: null, lastLogout: null})
196+
db.insertOne({username: username, password: hashedPassword, canvasUser: null, lastLogin: Date.now(), lastLogout: null})
197197
return res.status(200).json({message: "User registered"});
198198
});
199199

Frontend/src/App.jsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import './App.css'
44
import Login from "./Login"
55

66
const API_URL = "http://localhost:3500"
7-
const API_KEY = "nolol"
7+
// const API_KEY = "nolol"
88

99
function App() {
1010
const [courses, setCourses] = useState(null);
11-
const [userId, setUserId] = useState(null);
12-
const [userData, setUserData] = useState(null);
11+
const [userId, setUserId] = useState(null); //canvas user id
12+
const [userData, setUserData] = useState(null); //from db
1313
const [loginTime, setLoginTime] = useState(null);
14+
const [apiKey, setApiKey] = useState(null)
1415

1516
//due_at, points_possible, has_submitted_submissions, name,
1617

@@ -36,7 +37,7 @@ function App() {
3637
const getUserData = async () => {
3738
const res = await axios.get(`${API_URL}/getUser`, {
3839
params: {
39-
"canvas_api_token": API_KEY
40+
"canvas_api_token": apiKey
4041
}
4142
})
4243
await setUserId(res.data.id)
@@ -65,19 +66,19 @@ function App() {
6566
}, [])
6667

6768
useEffect(() => {
68-
const loginUser = async () => {
69-
if (!userId) {return}
69+
// const loginUser = async () => {
70+
// if (!userId) {return}
7071

71-
const res = await axios.get(`${API_URL}/login`, {
72-
params: {
73-
"user_id": userId
74-
}
75-
})
76-
console.log(res.data.user)
77-
await setUserData(res.data.user);
78-
}
72+
// const res = await axios.get(`${API_URL}/login`, {
73+
// params: {
74+
// "user_id": userId
75+
// }
76+
// })
77+
// console.log(res.data.user)
78+
// await setUserData(res.data.user);
79+
// }
7980

80-
loginUser();
81+
// loginUser();
8182
}, [userId])
8283

8384
useEffect(() => {
@@ -86,7 +87,7 @@ function App() {
8687

8788
const res = await axios.get(`${API_URL}/getCourses`, {
8889
params: {
89-
"canvas_api_token": API_KEY
90+
"canvas_api_token": apiKey
9091
}
9192
})
9293

@@ -96,7 +97,7 @@ function App() {
9697
var newAssignments = [];
9798
const assignments = await axios.get(`${API_URL}/getAssignments`, {
9899
params: {
99-
"canvas_api_token": API_KEY,
100+
"canvas_api_token": apiKey,
100101
"course_id": c.id,
101102
}
102103
})
@@ -106,7 +107,7 @@ function App() {
106107
if (a.has_submitted_submissions) {
107108
var submission = await axios.get(`${API_URL}/getSubmission`, {
108109
params: {
109-
"canvas_api_token": API_KEY,
110+
"canvas_api_token": apiKey,
110111
"course_id": c.id,
111112
"assignment_id": a.id,
112113
"user_id": userId
@@ -132,12 +133,12 @@ function App() {
132133
}
133134

134135
getCourseData();
135-
}, [userData, userId])
136+
}, [userId, apiKey])
136137

137138

138139
return (
139140
<>
140-
<Login loginTime={setLoginTime}/>
141+
<Login setLoginTime={setLoginTime} apiKey={apiKey} setApiKey={setApiKey} setUserData={setUserData}/>
141142
<h1>Your course info {userId ? <>(UID: {userId})</> : <></>}</h1>
142143
{courses && courses.message != "No courses available" ?
143144
courses.map((c) => {

Frontend/src/Login.jsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import "./css/Login.css"
66

77
const API_URL = "http://localhost:3500"
88

9-
export default function Login({setLoginTime}) {
9+
export default function Login({setLoginTime, apiKey, setApiKey, setUserData}) {
1010
const [username, setUser] = useState("");
1111
const [password, setPassword] = useState("");
12-
const [canvasAPIKey, setCanvasAPIKey] = useState("");
1312
const [logState, setLogState] = useState("login");
1413

1514
const handleSubmit = (e) => {
@@ -23,13 +22,19 @@ export default function Login({setLoginTime}) {
2322
setLoginTime(Date.now());
2423

2524
if (logState === "login") {
26-
const temp = await axios.get(`${API_URL}/loginUser`, {
27-
params: {
28-
"username": username,
29-
"password": password
30-
}
31-
})
32-
console.log(temp)
25+
try {
26+
const temp = await axios.get(`${API_URL}/loginUser`, {
27+
params: {
28+
"username": username,
29+
"password": password
30+
}
31+
})
32+
33+
setApiKey(localStorage.getItem("canvasAPIKey"))
34+
setUserData(temp.data)
35+
} catch (e) {
36+
console.log(e);
37+
}
3338

3439
} else if (logState === "create") {
3540
try {
@@ -39,11 +44,13 @@ export default function Login({setLoginTime}) {
3944
"password": password
4045
}
4146
})
42-
localStorage.setItem("canvasAPIKey", canvasAPIKey)
47+
localStorage.setItem("canvasAPIKey", apiKey)
48+
49+
//LOG IN THE USER AS WELL
4350

4451
console.log(temp)
4552
} catch (e) {
46-
console.log("account creation failed")
53+
console.log("account creation failed") //TELL THE USER IF THE USERNAME IS TAKEN
4754
}
4855
}
4956
}
@@ -74,8 +81,8 @@ export default function Login({setLoginTime}) {
7481
type="password"/>
7582
</div>
7683
{
77-
logState === "create" && <CanvasAPIKeyContainer keyVal={canvasAPIKey}
78-
setKey={setCanvasAPIKey}/>
84+
logState === "create" && <CanvasAPIKeyContainer keyVal={apiKey}
85+
setKey={setApiKey}/>
7986
}
8087
<button type="submit">{logState === "login" ? "Login" : "Create Account"}</button>
8188
</form>
@@ -106,5 +113,8 @@ CanvasAPIKeyContainer.propTypes = {
106113
setKey: PropTypes.func
107114
}
108115
Login.propTypes = {
109-
setLoginTime: PropTypes.func
116+
setLoginTime: PropTypes.func,
117+
apiKey: PropTypes.string,
118+
setApiKey: PropTypes.func,
119+
setUserData: PropTypes.func
110120
}

0 commit comments

Comments
 (0)