Skip to content

Commit

Permalink
docs: Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
JorWo committed Nov 8, 2024
1 parent ab639a6 commit e408c8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Currently supports CAS 2.0, CAS 3.0 and SAML 1.1 service validation methods.
- [FAQ](#faq)
- [Is there support for other React frameworks like Vite and Remix?](#is-there-support-for-other-react-frameworks-like-vite-and-remix)
- [How do I use `getCurrentUser()` and `isLoggedIn()` in a client component?](#how-do-i-use-getcurrentuser-and-isloggedin-in-a-client-component)
- [How do I resolve the error when importing `login()` and `logout()` from `'next-cas-client/client'`?](#how-do-i-resolve-the-error-when-importing-login-and-logout-from-next-cas-clientclient)
- [How do I resolve the error when importing `getCurrentUser()` and `isLoggedIn()` from `'next-cas-client/app'` or `'next-cas-client/pages'`?](#how-do-i-resolve-the-error-when-importing-getcurrentuser-and-isloggedin-from-next-cas-clientapp-or-next-cas-clientpages)

## Getting Started

Expand Down Expand Up @@ -68,17 +68,19 @@ NEXT_CAS_CLIENT_SECRET=GenerateA32CharacterLongPassword
**App Router:** `app/api/cas/[client]/route.ts`:

```ts
import { handleAuth, ValidatorProtocol } from 'next-cas-client';
import { ValidatorProtocol } from 'next-cas-client';
import { handleAuth } from 'next-cas-client/app';

export const GET = handleAuth({ validator: ValidatorProtocol.SAML11 });
export const GET = handleAuth({ validator: ValidatorProtocol.CAS30 });
```

**Page Router:** `pages/api/cas/[client].ts`:

```ts
import { handleAuth, ValidatorProtocol } from 'next-cas-client';
import { ValidatorProtocol } from 'next-cas-client';
import { handleAuth } from 'next-cas-client/pages';

export default handleAuth({ validator: ValidatorProtocol.SAML11 });
export default handleAuth({ validator: ValidatorProtocol.CAS30 });
```

**handleAuth() Options:**
Expand All @@ -90,7 +92,7 @@ export default handleAuth({ validator: ValidatorProtocol.SAML11 });
- `loadUser` _(Optional)_: Function to redefine the user object stored in session.

- Parameters: `casUser: CasUser`
- Returns: `any | promise<any>`
- Returns: `any | Promise<any>`
- If a `loadUser` function is not passed in, the stored user in session defaults to a object of type `CasUser`:
```ts
type CasUser = {
Expand Down Expand Up @@ -127,7 +129,7 @@ Visits the CAS login page.
```jsx
'use client';
import { login } from 'next-cas-client/client';
import { login } from 'next-cas-client';
<button onClick={() => login()}>Login</button>;
```
Expand All @@ -143,7 +145,7 @@ Visits the CAS logout page.
```jsx
'use client';
import { login } from 'next-cas-client/client';
import { login } from 'next-cas-client';
<button onClick={() => logout()}>Logout</button>;
```
Expand All @@ -156,10 +158,24 @@ import { login } from 'next-cas-client/client';

Gets the current user. Returns `null` if no user is logged-in.

**App Router:**

```ts
import { getCurrentUser } from 'next-cas-client/app';
const currentUser = await getCurrentUser();
```

**Page Router:**

```ts
import { getCurrentUser } from 'next-cas-client/pages';
export const getServerSideProps = (async (context) => {
return { props: { currentUser: await getCurrentUser(context) } };
}) satisfies GetServerSideProps<{ currentUser: CasUser | null }>;
```

Returns an object of type `CasUser` by default. Define the generic type of `getCurrentUser()` if you used the `loadUser` option in `handleAuth()`. The type should match the return of the `loadUser` function you defined.

- Example:
Expand All @@ -179,10 +195,24 @@ Returns an object of type `CasUser` by default. Define the generic type of `getC

Returns `true` if a user is logged-in.

**App Router:**

```ts
import { getCurrentUser } from 'next-cas-client/app';
const isLoggedIn = await isLoggedIn();
```

**Page Router:**

```ts
import { getCurrentUser } from 'next-cas-client/pages';
export const getServerSideProps = (async (context) => {
return { props: { isLoggedIn: await isLoggedIn(context) } };
}) satisfies GetServerSideProps<{ isLoggedIn: boolean }>;
```

## Examples

A fully functional demo is available using Docker.
Expand Down Expand Up @@ -213,6 +243,6 @@ No, not at this moment.

It is recommended to use those functions inside a server component then pass them as props into a client component.

### How do I resolve the error when importing `login()` and `logout()` from `'next-cas-client/client'`?
### How do I resolve the error when importing `getCurrentUser()` and `isLoggedIn()` from `'next-cas-client/app'` or `'next-cas-client/pages'`?

In your project's `tsconfig.json`, set `compilerOptions.moduleResolution` to "bundler".
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-cas-client",
"version": "1.2.0",
"version": "1.2.1",
"description": "A CAS client built for Next.js to authenticate, validate tickets, and provide session management to a CAS server",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down

0 comments on commit e408c8e

Please sign in to comment.