Generate Oauth/Social sign in links server side

I'm just getting started with better-auth and have what might be a basic question. I want to generate a sign in link on my server. What is the correct API method to do that? e.g. I want a URL for a "sign in to google" button that I can include in my server side template (either in a form as the form action, or as a link URL)
Solution:
You do need to follow the setup guide for the authentication provider you wish to use: https://www.better-auth.com/docs/authentication/google all client calls can be access on the auth.api and the options you would usally pass to the client call like: ```ts...
Google | Better Auth
Google provider setup and usage.
API | Better Auth
Better Auth API.
Jump to solution
2 Replies
lonelyplanet
lonelyplanet3mo ago
Next.js example for microsoft
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';

const res = await auth.api.signInSocial({
headers: await headers(),
body: {
provider: 'microsoft',
callbackURL: '/',
},
});
const authUrl = res.url; // type is string | undefined, eg. https://login.microsoftonline.com/..
import { auth } from '@/lib/auth';
import { headers } from 'next/headers';

const res = await auth.api.signInSocial({
headers: await headers(),
body: {
provider: 'microsoft',
callbackURL: '/',
},
});
const authUrl = res.url; // type is string | undefined, eg. https://login.microsoftonline.com/..
Solution
lonelyplanet
lonelyplanet3mo ago
You do need to follow the setup guide for the authentication provider you wish to use: https://www.better-auth.com/docs/authentication/google all client calls can be access on the auth.api and the options you would usally pass to the client call like:
const data = await authClient.signIn.social({
provider: "google",
idToken: {
token: // Google ID Token,
accessToken: // Google Access Token
}
})
const data = await authClient.signIn.social({
provider: "google",
idToken: {
token: // Google ID Token,
accessToken: // Google Access Token
}
})
becomes
const data = await auth.signInSocial({
headers: headers, // you need to pass in the request headers
body:{
provider: "google",
idToken: {
token: // Google ID Token,
accessToken: // Google Access Token
}
}
})
const data = await auth.signInSocial({
headers: headers, // you need to pass in the request headers
body:{
provider: "google",
idToken: {
token: // Google ID Token,
accessToken: // Google Access Token
}
}
})
see more documentation here
Google | Better Auth
Google provider setup and usage.
API | Better Auth
Better Auth API.

Did you find this page helpful?