vintage-auth 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +0 -0
- package/index.js +19 -0
- package/models/User.js +9 -0
- package/package.json +13 -0
- package/routes/auth.js +63 -0
package/.env
ADDED
|
File without changes
|
package/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const express = require('express')
|
|
2
|
+
const cors = require('cors')
|
|
3
|
+
const vintageAuth = require('./index') // wait, your first file is the package itself?
|
|
4
|
+
|
|
5
|
+
// If your first code block IS index.js, then you use it like this:
|
|
6
|
+
|
|
7
|
+
const app = express()
|
|
8
|
+
app.use(cors())
|
|
9
|
+
app.use(express.json())
|
|
10
|
+
|
|
11
|
+
// Mount vintage-auth
|
|
12
|
+
const authRouter = vintageAuth({
|
|
13
|
+
mongoUri: 'mongodb://localhost:27017/vintage',
|
|
14
|
+
jwtSecret: 'supersecret_dev_key'
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
app.use('/api', authRouter) // now routes = /api/auth/register
|
|
18
|
+
|
|
19
|
+
app.listen(3001, () => console.log('Backend on 3001'))
|
package/models/User.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const mongoose = require('mongoose')
|
|
2
|
+
|
|
3
|
+
const userSchema = new mongoose.Schema({
|
|
4
|
+
email: { type: String, required: true, unique: true },
|
|
5
|
+
password: { type: String, required: true },
|
|
6
|
+
createdAt: { type: Date, default: Date.now }
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
module.exports = mongoose.model('User', userSchema)
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vintage-auth",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Drop-in auth system for Express apps",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": ["auth", "express", "jwt", "mongodb"],
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"bcryptjs": "^2.4.3",
|
|
10
|
+
"jsonwebtoken": "^9.0.0",
|
|
11
|
+
"mongoose": "^8.0.0"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/routes/auth.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const express = require('express')
|
|
2
|
+
const bcrypt = require('bcryptjs')
|
|
3
|
+
const jwt = require('jsonwebtoken')
|
|
4
|
+
const User = require('../models/User')
|
|
5
|
+
|
|
6
|
+
const router = express.Router()
|
|
7
|
+
// routes/auth.js - top of /register
|
|
8
|
+
if (!email || !password)
|
|
9
|
+
return res.status(400).json({ message: 'Email and password required' })
|
|
10
|
+
// POST /auth/register
|
|
11
|
+
router.post('/register', async (req, res) => {
|
|
12
|
+
try {
|
|
13
|
+
const { email, password } = req.body
|
|
14
|
+
|
|
15
|
+
const existing = await User.findOne({ email })
|
|
16
|
+
if (existing) return res.status(400).json({ message: 'Email already exists' })
|
|
17
|
+
|
|
18
|
+
const hashed = await bcrypt.hash(password, 10)
|
|
19
|
+
const user = await User.create({ email, password: hashed })
|
|
20
|
+
|
|
21
|
+
const token = jwt.sign({ id: user._id }, process.env.VINTAGE_JWT_SECRET, { expiresIn: '7d' })
|
|
22
|
+
|
|
23
|
+
res.json({ token, user: { id: user._id, email } })
|
|
24
|
+
} catch (err) {
|
|
25
|
+
res.status(500).json({ message: 'Server error' })
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// POST /auth/login
|
|
30
|
+
router.post('/login', async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const { email, password } = req.body
|
|
33
|
+
|
|
34
|
+
const user = await User.findOne({ email })
|
|
35
|
+
if (!user) return res.status(400).json({ message: 'Invalid credentials' })
|
|
36
|
+
|
|
37
|
+
const match = await bcrypt.compare(password, user.password)
|
|
38
|
+
if (!match) return res.status(400).json({ message: 'Invalid credentials' })
|
|
39
|
+
|
|
40
|
+
const token = jwt.sign({ id: user._id }, process.env.VINTAGE_JWT_SECRET, { expiresIn: '7d' })
|
|
41
|
+
|
|
42
|
+
res.json({ token, user: { id: user._id, email } })
|
|
43
|
+
} catch (err) {
|
|
44
|
+
res.status(500).json({ message: 'Server error' })
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
// GET /auth/me
|
|
49
|
+
router.get('/me', async (req, res) => {
|
|
50
|
+
try {
|
|
51
|
+
const token = req.headers.authorization?.split(' ')[1]
|
|
52
|
+
if (!token) return res.status(401).json({ message: 'No token' })
|
|
53
|
+
|
|
54
|
+
const decoded = jwt.verify(token, process.env.VINTAGE_JWT_SECRET)
|
|
55
|
+
const user = await User.findById(decoded.id).select('-password')
|
|
56
|
+
|
|
57
|
+
res.json(user)
|
|
58
|
+
} catch {
|
|
59
|
+
res.status(401).json({ message: 'Invalid token' })
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
module.exports = router
|