-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sidebar, protected route, persisted state in frontend and authe…
…ntication and database models in backend
- Loading branch information
1 parent
0165ddd
commit dd2fe80
Showing
25 changed files
with
4,762 additions
and
592 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const chatSchema = new mongoose.Schema({ | ||
messages: [ | ||
{ | ||
message: {type: String}, | ||
sentBy: {type: String, required: true, enum: ["bot", "user"]}, | ||
}, | ||
], | ||
}); | ||
|
||
const Chat = mongoose.model('Chat', chatSchema); | ||
|
||
module.exports = Chat |
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,38 @@ | ||
const mongoose = require("mongoose"); | ||
const bcrypt = require("bcrypt"); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
email: {type: String, required: true, unique: true}, | ||
username: {type: String, required: true, unique: true}, | ||
password: {type: String, required: true}, | ||
chats: [{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Chat', | ||
}] | ||
}); | ||
|
||
userSchema.pre("save", function (next) { | ||
const user = this; | ||
bcrypt.hash(user.password, 10, (err, hashedPassword) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
user.password = hashedPassword; | ||
next(); | ||
}); | ||
}); | ||
|
||
userSchema.methods.comparePassword = function(candidatePassword, cb) { | ||
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => { | ||
if (err) { | ||
cb(err); | ||
} | ||
|
||
cb(null, isMatch); | ||
}) | ||
} | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
module.exports = User; |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const express = require("express"); | ||
const User = require("../model/UserSchema"); | ||
const { createJSONToken } = require("../util/auth"); | ||
const router = express.Router(); | ||
|
||
router.post('/login', async (req, res) => { | ||
const { username, password } = req.body; | ||
const user = await User.findOne({ username }); | ||
if (!user) { | ||
return res.status(401).json({ message: 'Invalid credentials' }); | ||
} | ||
user.comparePassword(password, (err, isMatch) => { | ||
if (err) { | ||
return res.status(401).json({ message: 'Invalid credentials' }); | ||
} | ||
if (!isMatch) { | ||
return res.status(401).json({ message: 'Invalid credentials' }); | ||
} | ||
|
||
const userID = user._id; | ||
const token = createJSONToken({userID}); | ||
|
||
res.status(200).json({ | ||
token: token, | ||
}) | ||
}); | ||
}); | ||
|
||
router.post('/signup', async (req, res) => { | ||
const {email, username, password, confirmPassword} = req.body; | ||
|
||
|
||
try { | ||
const user = new User({ | ||
username, | ||
email, | ||
password, | ||
chats: [], | ||
}) | ||
|
||
console.log(user); | ||
await user.save(); | ||
|
||
res.status(201).json({message: 'User created'}); | ||
} catch (err) { | ||
res.status(401).json({message: err.message}); | ||
} | ||
}) | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const express = require("express"); | ||
const User = require("../model/UserSchema"); | ||
const router = express.Router(); | ||
|
||
router.post('/', (req, res) => { | ||
|
||
|
||
}) |
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,14 @@ | ||
const { sign, verify } = require("jsonwebtoken"); | ||
|
||
const KEY = process.env.SECRET_KEY; | ||
|
||
function createJSONToken(userID) { | ||
return sign({userID}, KEY, {expiresIn: '1h'}); | ||
} | ||
|
||
function validateJSONToken(token) { | ||
return verify(token, secret); | ||
} | ||
|
||
exports.createJSONToken = createJSONToken; | ||
exports.validateJSONTOken = validateJSONToken; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
import {createBrowserRouter, RouterProvider} from "react-router-dom"; | ||
import AuthPage from "./pages/AuthPage"; | ||
import AuthPage, {action as authAction} from "./pages/AuthPage"; | ||
import Private from "./util/Private"; | ||
import ChatPage from "./pages/ChatPage"; | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
element: <ChatPage />, | ||
// errorElement: <ErrorPage />, | ||
children: [], | ||
}, | ||
{ | ||
path: "/login", | ||
element: <AuthPage mode="login"/>, | ||
}, | ||
{ | ||
path: "/signup", | ||
element: <AuthPage mode="signup"/>, | ||
}, | ||
{ | ||
path: "/chat/:chatId", | ||
element: <ChatPage />, | ||
}, | ||
{ | ||
path: "/", | ||
element: <ChatPage />, | ||
// errorElement: <ErrorPage />, | ||
children: [], | ||
}, | ||
{ | ||
path: "/login", | ||
action: authAction, | ||
element: <AuthPage mode="login" />, | ||
}, | ||
{ | ||
path: "/signup", | ||
action: authAction, | ||
element: <AuthPage mode="signup" />, | ||
}, | ||
{ | ||
path: "/chat/:chatId", | ||
element: ( | ||
<Private> | ||
<ChatPage /> | ||
</Private> | ||
), | ||
}, | ||
]); | ||
|
||
function App() { | ||
return <RouterProvider router={router} />; | ||
return <RouterProvider router={router} />; | ||
} | ||
|
||
export default App; |
Oops, something went wrong.