create-appncy
Version:
Create projects as goncy would
114 lines (96 loc) • 2.73 kB
Markdown
Instructions on how to setup Kinde auth on the app.
[](https://kinde.com/).
Install the Kinde dependency for Next.js:
```bash
pnpm add @kinde-oss/kinde-auth-nextjs
```
Once you created the project you should see below all the environment variables needed for the project. Copy all of them and add them to your `.env.local` file.
```bash
KINDE_CLIENT_ID=
KINDE_CLIENT_SECRET=
KINDE_ISSUER_URL=
KINDE_SITE_URL=
KINDE_POST_LOGOUT_REDIRECT_URL=
KINDE_POST_LOGIN_REDIRECT_URL=
```
Kinde has several helpers to get the user session and permissions. Also, it has some components to handle the login and register links.
```tsx
import {RegisterLink, LoginLink} from "@kinde-oss/kinde-auth-nextjs/components";
...
<LoginLink>Sign in</LoginLink>
<RegisterLink>Sign up</RegisterLink>
```
```ts
const {
getAccessToken,
getBooleanFlag,
getFlag,
getIdToken,
getIntegerFlag,
getOrganization,
getPermission,
getPermissions,
getStringFlag,
getUser,
getUserOrganizations,
isAuthenticated
} = getKindeServerSession();
console.log(await getAccessToken());
console.log(await getBooleanFlag("bflag", false));
console.log(await getFlag("flag", "x", "s"));
console.log(await getIntegerFlag("iflag", 99));
console.log(await getOrganization());
console.log(await getPermission("eat:chips"));
console.log(await getPermissions());
console.log(await getStringFlag("sflag", "test"));
console.log(await getUser());
console.log(await getUserOrganizations());
console.log(await isAuthenticated());
```
```ts
const {
permissions,
isLoading,
user,
accessToken,
organization,
userOrganizations,
getPermission,
getBooleanFlag,
getIntegerFlag,
getFlag,
getStringFlag,
getClaim,
getAccessToken,
getToken,
getIdToken,
getOrganization,
getPermissions,
getUserOrganizations
} = useKindeBrowserClient();
console.log(getPermission("eat:chips"));
console.log(getBooleanFlag("flag", false));
console.log(getIntegerFlag("eat:chips", 1));
console.log(getStringFlag("eat:chips", "ds"));
console.log(getFlag("eat:chips", false, "b"));
console.log("accessToken", accessToken);
console.log(getClaim("aud"));
```
```ts
import {withAuth} from "@kinde-oss/kinde-auth-nextjs/middleware";
export default function middleware(req) {
return withAuth(req);
}
export const config = {
matcher: ["/admin"]
};
```