Commit 96f0d097 authored by TokaKaram's avatar TokaKaram

admin functions test

parent 93da2368
/node_modules
/.env
\ No newline at end of file
/.env
/Lec7.drawio
\ No newline at end of file
......@@ -41,6 +41,7 @@ exports.login = async (req, res) => {
}
const user = users[0];
const isMatch = await bcrypt.compare(password, user.password);
// console.log
if (!isMatch) {
return res.status(400).json({ message: "Invalid credentials" });
}
......
......@@ -3,13 +3,16 @@ const pool = require("../config/database");
module.exports = async (req, res, next) => {
try {
console.log("token", req.header("Authorization"));
const token = req.header("Authorization")?.replace("Bearer ", "");
console.log("token without bearer", token);
if (!token) {
return res
.status(401)
.json({ message: "No token, authorization denied" });
}
const decoded = jwt.verify(token, process.env.JWT_SECRET);
console.log("decoded", decoded);
const [users] = await pool.execute(
"SELECT id, username, email, role FROM users WHERE id = ?",
[decoded.id]
......@@ -20,6 +23,7 @@ module.exports = async (req, res, next) => {
req.user = users[0];
next();
} catch (error) {
res.status(401).json({ message: "Token is not valid" });
console.log("error", error);
res.status(401).json({ message: "Token is not valid error" });
}
};
......@@ -16,7 +16,7 @@ export default function Navbar() {
<div className="container mx-auto px-4">
<div className="flex justify-between items-center h-16">
<Link to="/" className="text-2xl font-bold text-indigo-600">MCQ App</Link>
{user ? (
{user ? (
<div className="flex items-center gap-6">
<Link to="/" className="flex items-center gap-2 text-gray-600 hover:text-indigo-600 transition">
<FiHome /> Quizzes
......
import { useMemo, useState } from "react";
export default function QuestionCard({ scenario, selectedAnswer, onSelectAnswer, showResult, answerResult }) {
const givensTable = typeof scenario.givens_table === 'string' ? JSON.parse(scenario.givens_table) : scenario.givens_table;
const options = [
const options = useMemo(() =>{
return[
{ text: scenario.best_answer, isCorrect: true },
{ text: scenario.other_option1, explanation: scenario.other_option1_exp },
{ text: scenario.other_option2, explanation: scenario.other_option2_exp },
{ text: scenario.other_option3, explanation: scenario.other_option3_exp }
].sort(() => Math.random() - 0.5);
} ,[scenario]);
const [active,setActive]=useState("no answer selected");
const getOptionClass = (option) => {
// console.log("option", option);
let baseClass = 'quiz-option p-4 rounded-lg border-2 cursor-pointer mb-3';
if (!showResult) {
return `${baseClass} ${selectedAnswer === option.text ? 'selected' : 'border-gray-200'}`;
return `${baseClass} ${active === option.text ? 'border-blue-200' : 'border-gray-200'}`;
}
if (option.text === answerResult?.correctAnswer) {
return `${baseClass} correct`;
......@@ -49,7 +54,7 @@ export default function QuestionCard({ scenario, selectedAnswer, onSelectAnswer,
<h3 className="font-semibold text-gray-700 mb-3">Select your answer:</h3>
<div>
{options.map((option, index) => (
<div key={index} className={getOptionClass(option)} onClick={() => !showResult && onSelectAnswer(option.text)}>
<div key={index} className={[getOptionClass(option)] } onClick={() =>{ !showResult && onSelectAnswer(option.text); console.log("Clicked");setActive(option.text)}}>
<p className="text-gray-800">{option.text}</p>
</div>
))}
......
......@@ -15,8 +15,12 @@ export default function Login() {
e.preventDefault();
setLoading(true);
try {
await login(email, password);
const r=await login(email, password);
console.log("r=>",r);
toast.success('Login successful!');
if(r.user.role === 'admin')
navigate('/admin');
else
navigate('/');
} catch (error) {
toast.error(error.response?.data?.message || 'Login failed');
......
......@@ -136,9 +136,7 @@
import axios from "axios";
const api = axios.create({
baseURL:
import.meta.env.VITE_API_URL ||
"https://scenario-api.caprover.al-arcade.com/api",
baseURL: "https://scenario-api.caprover.al-arcade.com/api/",
headers: { "Content-Type": "application/json" },
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment