@juliodevelop/auth
v0.1.0
Published
Auth toolkit com NextAuth v5, JWT utilities, middleware e hooks React
Downloads
55
Readme
@juliodevelop/auth
Auth toolkit para Next.js com NextAuth v5, middleware de rotas, hooks React, JWT e password hashing.
Instalação
npm install @juliodevelop/auth
# peer deps:
npm install next next-auth reactRequer
.npmrcconfigurado com o registry@juliodevelop. Veja o README raiz.
Sub-exports
@juliodevelop/auth → config + middleware
@juliodevelop/auth/react → AuthProvider + hooks
@juliodevelop/auth/jwt → sign, verify, decode JWT
@juliodevelop/auth/password → hash + verify senhaUso
Config NextAuth
import { createAuthConfig } from '@juliodevelop/auth'
export const authConfig = createAuthConfig({
providers: {
credentials: {
authorize: async (credentials) => {
const user = await db.user.findUnique({ where: { email: credentials.email } })
if (!user) return null
return { id: user.id, email: user.email, role: user.role }
},
},
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
},
pages: { signIn: '/login' },
})
export const { auth, signIn, signOut, handlers } = authConfigMiddleware
import { createAuthMiddleware } from '@juliodevelop/auth'
// Tudo protegido por padrão, exceto publicRoutes
export default createAuthMiddleware({
publicRoutes: ['/', '/login', '/register'],
roleRoutes: {
'/admin(.*)': ['admin'],
'/seller(.*)': ['admin', 'seller'],
},
})Hooks React
import { AuthProvider, useAuth } from '@juliodevelop/auth/react'
// No layout:
<AuthProvider><App /></AuthProvider>
// Em componentes:
const { user, isAuthenticated, hasRole, signOut } = useAuth()JWT (standalone, sem Next.js)
import { signJwt, verifyJwt, decodeJwt } from '@juliodevelop/auth/jwt'
const token = await signJwt({ userId: '123' }, { secret: 'key', expiresIn: '7d' })
const payload = await verifyJwt(token, 'key')Password (standalone, sem Next.js)
import { hashPassword, verifyPassword } from '@juliodevelop/auth/password'
const hashed = await hashPassword('senha123')
const isValid = await verifyPassword('senha123', hashed)