Skip to content

Instantly share code, notes, and snippets.

@abitbetterthanyesterday
Last active December 13, 2023 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abitbetterthanyesterday/f2a3b3a892e1b12eb77a6f7bee91f074 to your computer and use it in GitHub Desktop.
Save abitbetterthanyesterday/f2a3b3a892e1b12eb77a6f7bee91f074 to your computer and use it in GitHub Desktop.
SST AuthStack with NextJS

The url will be available at:

${Api.api.url} which will give you a list of your provider (here, only google). Each provider will have an auth link.

// auth.ts
import { AuthHandler, GoogleAdapter } from "sst/node/auth";
export const handler = AuthHandler({
providers: {
google: GoogleAdapter({
mode: "oidc",
clientID: "XXXX",
onSuccess: async (tokenset) => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello World",
}),
};
},
}),
},
});
// AuthStack.ts
import { Api, Auth, Stack } from "sst/constructs";
export function AuthStack(stack: Stack) {
const auth = new Auth(stack, "auth", {
authenticator: {
handler: "functions/auth.handler",
},
});
const api = new Api(stack, "api", {});
auth.attach(stack, { api, prefix: "/auth" });
return {
api,
};
}
//sst.config.ts
export default {
config(_input) {
return {
/** */
};
},
stacks(app) {
app.stack(function Site({ stack }) {
const { api } = AuthStack(stack);
const site = new NextjsSite(stack, "site", {
bind: [api],
environment: {
/** */
},
});
stack.addOutputs({
SiteUrl: site.url,
ApiUrl: api.url,
});
});
},
} satisfies SSTConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment