Skip to content

Commit

Permalink
Update to Next.js 14
Browse files Browse the repository at this point in the history
  • Loading branch information
backjonas committed May 14, 2024
1 parent 0c40026 commit fea3944
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 470 deletions.
40 changes: 2 additions & 38 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
import NextAuth, { NextAuthOptions } from 'next-auth'
import KeycloakProvider from 'next-auth/providers/keycloak'
import { API_URL } from '@lib/strapi'

export const authOptions: NextAuthOptions = {
debug: false,
providers: [
KeycloakProvider({
clientId: process.env.KEYCLOAK_CLIENT_ID ?? 'strapi',
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET ?? 'strapi',
issuer: process.env.KEYCLOAK_ISSUER,
httpOptions: {
timeout: 10000,
},
}),
],
session: {
strategy: 'jwt',
},
callbacks: {
async session({ session, token }) {
session.user.token = token.jwt
return session
},
async jwt({ token, user, account }) {
if (user) {
const response = await fetch(
`${API_URL}/auth/keycloak/callback?access_token=${account?.access_token}`
)
if (response.ok) {
const data = await response.json()
token.jwt = data.jwt
}
}
return token
},
},
}
import NextAuth from 'next-auth'
import { authOptions } from '@lib/nextauth'

const handler = NextAuth(authOptions)

Expand Down
2 changes: 1 addition & 1 deletion app/medlem/[privatePage]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getServerSession } from 'next-auth'
import { fetchPrivatePage } from '@lib/strapi/privatepage'
import Page from '@components/Page'
import { authOptions } from '../../api/auth/[...nextauth]/route'
import { authOptions } from '@lib/nextauth'
import Unauthorized from '@components/Unauthorized'

const PrivatePage = async ({ params }: { params: { privatePage: string } }) => {
Expand Down
37 changes: 37 additions & 0 deletions lib/nextauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NextAuthOptions } from 'next-auth'
import KeycloakProvider from 'next-auth/providers/keycloak'
import { API_URL } from '@lib/strapi'

export const authOptions: NextAuthOptions = {
providers: [
KeycloakProvider({
clientId: process.env.KEYCLOAK_CLIENT_ID ?? 'strapi',
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET ?? 'strapi',
issuer: process.env.KEYCLOAK_ISSUER,
httpOptions: {
timeout: 10000,
},
}),
],
session: {
strategy: 'jwt',
},
callbacks: {
async session({ session, token }) {
session.user.token = token.jwt
return session
},
async jwt({ token, user, account }) {
if (user) {
const response = await fetch(
`${API_URL}/auth/keycloak/callback?access_token=${account?.access_token}`
)
if (response.ok) {
const data = await response.json()
token.jwt = data.jwt
}
}
return token
},
},
}
10 changes: 6 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
module.exports = {
reactStrictMode: true,
images: {
domains: ['tf.fi', 'cms.tf.fi', 'localhost', 'test.tf.fi'],
remotePatterns: [
{ protocol: 'https', hostname: 'tf.fi' },
{ protocol: 'https', hostname: 'cms.tf.fi' },
{ protocol: 'https', hostname: 'test.tf.fi' },
{ protocol: 'http', hostname: 'localhost' },
],
},
output: 'standalone',
experimental: {
serverActions: true,
},
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"framer-motion": "^10.13.0",
"googleapis": "^137.1.0",
"marked": "^9.0.3",
"next": "^13.4.6",
"next": "^14.2.3",
"next-auth": "^4.23.1",
"qs": "^6.11.0",
"react": "^18.2.0",
Expand Down
Loading

0 comments on commit fea3944

Please sign in to comment.