-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JWT Authentication - Log in #6
Open
nicoquiroga90
wants to merge
6
commits into
main
Choose a base branch
from
Authentication-NicoQ
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b3e86fc
Divided scripts for start server-frontend
nicoquiroga90 b93a001
Merge branch 'main' of https://github.com/HackYourFuture-CPH/team26-T…
nicoquiroga90 f398dac
Refactoring of Main Components
nicoquiroga90 2041f80
added first jwt authentication to login
nicoquiroga90 f3e1674
Login context is added for accessing the dashboard
nicoquiroga90 82f18a9
Added time session and cookies token
nicoquiroga90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 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,18 @@ | ||
import { createContext, useContext, useState } from 'react'; | ||
|
||
const UserContext = createContext(); | ||
|
||
export const useUser = () => useContext(UserContext); | ||
|
||
export const UserProvider = ({ children }) => { | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
|
||
const login = () => setIsLoggedIn(true); | ||
const logout = () => setIsLoggedIn(false); | ||
|
||
return ( | ||
<UserContext.Provider value={{ isLoggedIn, login, logout }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; |
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,25 @@ | ||
const express = require('express'); | ||
const jwt = require('jsonwebtoken'); | ||
const db = require('../database'); | ||
const router = express.Router(); | ||
|
||
router.post('/', async (req, res) => { | ||
const { team_code } = req.body; | ||
|
||
try { | ||
const team = await db.select().from('teams').where('team_code', team_code).first(); | ||
|
||
if (!team) { | ||
return res.status(401).json({ error: 'Invalid team code' }); | ||
} | ||
|
||
const token = jwt.sign({ teamId: team.id }, process.env.JWT_SECRET, { expiresIn: '1h' }); | ||
|
||
res.json({ token }); | ||
} catch (error) { | ||
console.error('Error logging in:', error); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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,37 +1,27 @@ | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const db = require('../database'); | ||
const router = express.Router(); | ||
|
||
const app = express(); | ||
|
||
app.use(cors({ | ||
origin: 'http://localhost:4051', | ||
methods: ['GET', 'POST'], | ||
credentials: true | ||
})); | ||
|
||
app.use(express.json()); | ||
|
||
app.get('/api/teams', async (req, res) => { | ||
router.get('/', async (req, res) => { | ||
try { | ||
const teams = await db.select().from('team'); | ||
const teams = await db.select().from('teams'); | ||
res.json(teams); | ||
} catch (error) { | ||
console.error('Error fetching teams:', error); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
app.post('/api/teams', async (req, res) => { | ||
router.post('/', async (req, res) => { | ||
try { | ||
const { title, code } = req.body; // Aquí se espera que el campo se llame 'title' | ||
const { team_name, team_code } = req.body; | ||
const creationDate = new Date(); | ||
await db('team').insert({ title, code, created_date: creationDate }); // Insertar 'title' en lugar de 'name' | ||
await db('teams').insert({ team_name, team_code, created_date: creationDate }); | ||
res.status(201).json({ message: 'Team created successfully' }); | ||
} catch (error) { | ||
console.error('Error creating team:', error); | ||
res.status(500).json({ error: 'Internal Server Error' }); | ||
} | ||
}); | ||
|
||
module.exports = app; | ||
module.exports = router; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a specific reason for removing this? maybe we should update the readme for startup approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tried to unify the frontend and the backend start and make it run in different ports, but talking with other of the mentors we decided to keep it separate
"start-server": "node src/backend/server.js",
"start-frontend": "react-scripts start "