Skip to content

Commit

Permalink
updated next
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-honig committed May 10, 2024
1 parent bd65e85 commit ef0964d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 54 deletions.
@@ -0,0 +1,14 @@
import { getServerSession } from 'next-auth'
import { UserInfo } from 'remult'
import { remultNextApp } from 'remult/remult-next'
import { Task } from '../../../shared/task'
import { authOptions } from '../auth/[...nextauth]/auth'

export const api = remultNextApp({
entities: [Task],
admin: true,
// using next auth experimental version based on: https://codevoweb.com/setup-and-use-nextauth-in-nextjs-13-app-directory/
getUser: async () => {
return (await getServerSession(authOptions))?.user as UserInfo
},
})
@@ -1,16 +1,3 @@
import { getServerSession } from 'next-auth'
import { UserInfo } from 'remult'
import { remultNextApp } from 'remult/remult-next'
import { Task } from '../../../shared/task'
import { authOptions } from '../auth/[...nextauth]/route'
import { api } from './api'

export const api = remultNextApp({
entities: [Task],
admin: true,
// using next auth experimental version based on: https://codevoweb.com/setup-and-use-nextauth-in-nextjs-13-app-directory/
getUser: async () => {
return (await getServerSession(authOptions))?.user as UserInfo
},
})

export const { POST, PUT, DELETE, GET, withRemult } = api
export const { POST, PUT, DELETE, GET } = api
@@ -0,0 +1,36 @@
import NextAuth, { NextAuthOptions } from 'next-auth'
import Credentials from 'next-auth/providers/credentials'
import { UserInfo } from 'remult'

const validUsers: UserInfo[] = [
{ id: '1', name: 'Jane', roles: ['admin'] },
{ id: '2', name: 'Steve' },
]
export function getUserById(id: string | undefined) {
return validUsers.find((user) => user.id === id)
}

export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
providers: [
Credentials({
credentials: {
name: {
placeholder: 'Try Steve or Jane',
},
},
authorize: (info) =>
validUsers.find((user) => user.name === info?.name) || null,
}),
],
callbacks: {
session: ({ session, token }) => ({
...session,
user: getUserById(token?.sub),
}),
},
}

export const handler = NextAuth(authOptions)
@@ -1,37 +1,3 @@
import NextAuth, { NextAuthOptions } from 'next-auth'
import Credentials from 'next-auth/providers/credentials'
import { UserInfo } from 'remult'
import { handler } from './auth'

const validUsers: UserInfo[] = [
{ id: '1', name: 'Jane', roles: ['admin'] },
{ id: '2', name: 'Steve' },
]
export function getUserById(id: string | undefined) {
return validUsers.find((user) => user.id === id)
}

export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
providers: [
Credentials({
credentials: {
name: {
placeholder: 'Try Steve or Jane',
},
},
authorize: (info) =>
validUsers.find((user) => user.name === info?.name) || null,
}),
],
callbacks: {
session: ({ session, token }) => ({
...session,
user: getUserById(token?.sub),
}),
},
}

const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }
Expand Up @@ -2,7 +2,7 @@
import { createSchema, createYoga } from 'graphql-yoga'
import { remultGraphql } from 'remult/graphql'
import { Task } from '../../../shared/task'
import { api } from '../[...remult]/route'
import { api } from '../[...remult]/api'

const { typeDefs, resolvers } = remultGraphql({
entities: [Task],
Expand Down
@@ -1,13 +1,13 @@
import { NextResponse } from 'next/server'
import { remult } from 'remult'
import { Task } from '../../../shared/task'
import { withRemult } from '../[...remult]/route'
import { api } from '../[...remult]/api'

export const dynamic = 'force-dynamic'

export async function GET(req: Request) {
return NextResponse.json({
result: await withRemult(() => remult.repo(Task).count()),
user: await withRemult(async () => remult.user),
result: await api.withRemult(() => remult.repo(Task).count()),
user: await api.withRemult(async () => remult.user),
})
}

0 comments on commit ef0964d

Please sign in to comment.