Hi everyone,
There’s an issue that’s kinda driving me crazy and I’ve tried all the combinations of env variables, setups etc.
Setup:
- Localhost with next.js
- NextAuth with credentials
I’m developing locally at localhost:3000
and I have my instance of directus at secrey.mydomain.com
.
The public API works smoothly but when I log a user in from localhost:3000
and receive an accessToken and/or set the cookie, whenever I make an API request to either /users/me
or /items/my-collection
from localhost:3000
it won’t include the credentials.
This is my authorize function:
async authorize(credentials) {
try {
const res = await directus.login(credentials?.email as string, credentials?.password as string, {
mode: 'cookie',
});
console.log('*** res ***', res);
if (res.access_token) {
(await cookies()).set('directus_session_token', res.access_token, {
sameSite: 'none',
path: '/',
secure: false,
});
}
const user = await directus.request(readMe());
return {
id: user.id,
email: user.email,
first_name: user.first_name,
last_name: user.last_name,
accessToken: res.access_token as string,
};
} catch (error) {
console.error('Auth error:', error);
throw new Error('Authentication failed');
}
where directus
is the client I import from
import { createDirectus, rest, graphql, authentication } from '@directus/sdk';
const directus = createDirectus(process.env.NEXT_PUBLIC_BASE_URL as string)
.with(authentication('cookie', { credentials: 'include', autoRefresh: true }))
.with(rest({ credentials: 'include' }))
.with(graphql({ credentials: 'include' }));
export default directus;
where NEXT_PUBLIC_BASE_URL
is sub.mydomain.com
.
I have set on sub.mydomain.com
all the combinations of
SESSION_COOKIE_SECURE
SESSION_COOKIE_SAME_SITE
REFRESH_TOKEN_COOKIE_SECURE
REFRESH_TOKEN_COOKIE_SAME_SITE
REFRESH_TOKEN_COOKIE_DOMAIN
but tbf nothing seems to work.
These are the requests I’m trying:
const promises = [
axios.patch(
`${process.env.NEXT_PUBLIC_BASE_URL}/users/me`,
{ first_name: firstName, last_name: lastName },
{ withCredentials: true }
),
directus.request(
updateItem('user_portfolio', userPortfolio.id, { location, date_of_birth: dateOfBirth, availability })
),
];
/users/me
returns 401 with invalid credentials
/items/user_portfolio
returns 403 with forbidden
I’m at loss and not sure where to go next, if not passing the accessToken
to the axios
requests which I’d rather avoid, because I’d love for the cookie
to just work.
Also, the auto-refresh doesn’t seem to work.
Please tell me if there’s anything else you might need from me in terms of info about my setup.
Thanks a lot in advance!