Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/djimenezweb/mern-auth
https://github.com/djimenezweb/mern-auth
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/djimenezweb/mern-auth
- Owner: djimenezweb
- Created: 2024-11-13T11:25:27.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2024-11-14T21:40:28.000Z (about 2 months ago)
- Last Synced: 2024-11-14T22:28:29.426Z (about 2 months ago)
- Language: TypeScript
- Size: 124 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MERN Auth
## 🚧 Work in progress! 🚧
Basic auth system with role- and session-based authorization.
## Back-End
- Express
- Refresh and Access Tokens (JWT)
- Hashed passwords (bcrypt)
- HttpOnly Cookies (cookie-parser)
- MongoDB & Mongoose## Front-End
- Vite + React + TypeScript
- Shadcn/ui
- Tailwind CSS## Sample code
```javascript
async function signup(req, res) {
// Get username and password from Request
const { username, password } = req.body;// Send error if empty fields
if (!username || !password) {
return res
.status(409)
.json({ message: 'Username and password are required' });
}try {
// Check if username already exists to prevent duplicates
const duplicate = await User.findOne({ username });
if (duplicate) {
return res.status(409).json({ message: 'Username already exists' });
}// Hash password
const hashedPassword = await hashPassword(password);// Save username and hashed password to database
const user = await User.create({ username, password: hashedPassword });
const userId = user._id.toString();// Create new session and save it to database
const session = await Session.create({
userId,
valid: true,
userAgent: req.headers['user-agent'],
ip: req.ip,
updatedAt: new Date(),
createdAt: new Date(),
});
const sessionId = session._id.toString();// Generate Access and Refresh Tokens
const accessToken = generateAccessToken(userId, sessionId);
const refreshToken = generateRefreshToken(sessionId);// Send Cookies
res.cookie('accessToken', accessToken, cookiesOptions);
res.cookie('refreshToken', refreshToken, cookiesOptions);// Send Response
return res.status(201).json({ message: 'User created' });
} catch (error) {
console.error(error);
return res.status(500).json({ message: 'An error ocurred' });
}
}
```