Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd65e85
commit ef0964d
Showing
6 changed files
with
57 additions
and
54 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
projects/test-servers/next-app-server/src/app/api/[...remult]/api.ts
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 @@ | ||
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 | ||
}, | ||
}) |
17 changes: 2 additions & 15 deletions
17
projects/test-servers/next-app-server/src/app/api/[...remult]/route.ts
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,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 |
36 changes: 36 additions & 0 deletions
36
projects/test-servers/next-app-server/src/app/api/auth/[...nextauth]/auth.ts
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,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) |
36 changes: 1 addition & 35 deletions
36
projects/test-servers/next-app-server/src/app/api/auth/[...nextauth]/route.ts
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,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 } |
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
6 changes: 3 additions & 3 deletions
6
projects/test-servers/next-app-server/src/app/api/test/route.ts
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,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), | ||
}) | ||
} |