Deployment failing on aws amplify
Unanswered
Muhammed Khal posted this in #help-forum
Hai Guys,
I created nextjs app with the next auth backend by the graphql and it got deployed on aws amplify, it's working fine in local but not in deployed version
I created nextjs app with the next auth backend by the graphql and it got deployed on aws amplify, it's working fine in local but not in deployed version
117 Replies
import axios from "axios";
import { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { API_URL } from "@/lib/gql";
import { UserLoginDocument, type UserLoginMutation } from "@/gql/graphql";
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
maxAge: 24 * 60 * 60,
},
pages: {
signIn: "/en/login",
},
providers: [
CredentialsProvider({
id: "credentials",
name: "Credentials",
credentials: {
email: { type: "email", label: "Email" },
password: { type: "password", label: "Password" },
remember: { type: "bool", label: "Remember me" },
},
authorize: async (credentials: { email: string; password: string } | undefined) => {
const { email, password } = credentials || {};
const res = await axios.post<{ data: UserLoginMutation }>(API_URL, {
query: UserLoginDocument,
variables: { email, password },
});
if (res.status !== 200) {
return null;
}
if (res.data?.data.userLogin?.token) {
return {
id: email || "",
email,
...(res.data?.data.userLogin || {}),
};
} else {
return null;
}
},
}),
],
callbacks: {
jwt: async ({ account, token }) => {
if (account) {
token.accessToken = account.access_token;
}
return token;
},
},
};route.tsimport NextAuth from "next-auth";
import { authOptions } from "@/lib/auth";
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };Please tag me while answering so I can be notified, thanks
use that digest ID and check if a log with that id is present in your vercel logs
oh yeah, i accidentally wrote vercel logs. I meant the logs in general. Amplify must provide some logs that the app itself returns right?
[next-auth][error][NO_SECRET]
But I already configured my secret @Clown
well it cant find it. Recheck the name of the environment variable in your project settings and inside your code aswell
also check by creating a dummy variable whether you can even access environment variables or not
Yes I did and i'm able to access
import { UserLoginDocument, type UserLoginMutation } from "@/gql/graphql";
export const authOptions: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,I am using only in one place
i cant think of any other reason its not working tbh
unless this secret is somehow being called in a client component
Yes it;s calling in a client component but the parent component is a server
<Layout>
</RegisterForm>
<Layout>
</RegisterForm>
<Layout>
As you know registerform a client componnent
i dont think that should be a issue
In that blog it;s saying some sever issue?
if its a no secret error then its not the same issue i think.
did you check if your environment variable is being named correctly in project settings
did you check if your environment variable is being named correctly in project settings
i know you probably arent doing this but im going to ask this anyways, in production you dont use the .env file and dont push that file to git.
you have to set those in your amplify project setting
Yep, everything is correctly setup
I havem't added the aws-amplify
'use client';
import { Amplify } from 'aws-amplify';
import awsExports from '@/aws-exports';
Amplify.configure({ ...awsExports, ssr: true });
export default function Home() {
return (
{/* Render a login form for your users */}
)
}Something like that
@Muhammed Khal yo yo
Amplify is weird
I ran into this issue a while ago with next auth
One sec I have a solution
version: 1
frontend:
phases:
preBuild:
commands:
- yarn install --frozen-lockfile
build:
commands:
- touch .env
- echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> .env
- echo "NEXTAUTH_URL=$NEXTAUTH_URL" >> .env
- yarn run build
artifacts:
baseDirectory: .next
files:
- "**/*"
cache:
paths:
- node_modules/**/*modify the file as needed for your specific build system
sure
but the trick is to create an env file during deploy
do i need to install amplify on my nextjs app??
and then echo your variables into it
how can i do?
after defining them in your amplify env variables in the ui
do i need to install amplify on my nextjs app??
No you do not
The solution is above
create a file called
amplify.yml in your rootand modify it to your needs based on the file I provided
(if you dont use yarn for example)
Make sure NEXTAUTH_SECRET and NEXTAUTH_URL are defined in the env settings via the amplify ui
This same logic applies to ALL server side env variables when using amplify and nextjs
yes
Please let me know if it works
sure
@linesofcode I am not using yaml , but still do I need to create that file and do I required to install any dependencies?
@linesofcode Make sure NEXTAUTH_SECRET and NEXTAUTH_URL are defined in the env settings via the amplify ui
amplify ui mean, setting the env in amplify like we do in vercel?
Yes.
Yes I already did that one
Are you free now, so I can take some of your time>?
@linesofcode
I’m not free
I provided the solutions above
version: 1
frontend:
phases:
preBuild:
commands:
- pnpm install --frozen-lockfile
build:
commands:
- touch .env
- echo "NEXTAUTH_SECRET=$NEXTAUTH_SECRET" >> .env
- echo "NEXTAUTH_URL=$NEXTAUTH_URL" >> .env
- pnpm run build
artifacts:
baseDirectory: .next
files:
- "**/*"
cache:
paths:
- node_modules/**/*I did like that
Okay
And what’s the result?
Re-deploying
Still same @linesofcode
Are you sure you set the variables in the amplify ui for all branches?
I'm not sure then\
Are you using the app router?
With next auth?
Page routers?
i use amplify with page
btw your google client and secret, 99% should not be public
looking at that screenshot
I am using amplify with app and next auth
you're leaking them to the public
Deleted
if you're using them for oauth, make sure they are server only
just like the nextauth env variables
and add them in amplify.yml
idk what to tell you about the app router not working in amplify
they even warn you it's expiremtnal still
I haven't implemented the google auth yet, because trying to figure this issue
amplify does not support app router completly
@linesofcode they even warn you it's expiremtnal still
Is that mean, it supports only on page router
100% page router, not 100% app router
In that case I should use the aws auth right instead of the next auth?
i have no clue
no offense but amplify sucks
if you have the option to use something else, use it
@linesofcode no offense but amplify sucks
10000000% agreed
Vercel is really awesome
THanks
It'll works with amplify right?
Okay
Hai @linesofcode , thanks for the guide, after some tweaks to amplify and nextjs config it got worked
Nice!
Thanks for the help
what was the solution?
In commands, first I installed the pnpm globally then installing the frozen lockfile then in next.config I created a env object then I added the nextauthseceret , of course witth process.env.nextauthsecret
I see