Skip to content

Commit

Permalink
Merge pull request #3 from sahilsuman933/nextjs-example
Browse files Browse the repository at this point in the history
[Example] Added nextjs example using Trustauthx TS SDK
  • Loading branch information
Neon-20 authored Dec 16, 2023
2 parents b6f1f15 + 47bb8ad commit e89f811
Show file tree
Hide file tree
Showing 17 changed files with 3,936 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
API_KEY=
SECRET_KEY=
API_SECRET=
ORG_ID=
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,40 @@ node_modules
dist

.env

# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz
.idea
# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
3 changes: 3 additions & 0 deletions nextjs-example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_KEY=
API_SECRET=
ORG_ID=
3 changes: 3 additions & 0 deletions nextjs-example/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions nextjs-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Empty file added nextjs-example/app/globals.css
Empty file.
22 changes: 22 additions & 0 deletions nextjs-example/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'

const inter = Inter({ subsets: ['latin'] })

export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
7 changes: 7 additions & 0 deletions nextjs-example/app/login/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AuthClient from "@/util/AuthClient";


export async function GET(request: Request) {

return Response.redirect(AuthClient.generateUrl());
}
13 changes: 13 additions & 0 deletions nextjs-example/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from "next/link";

export default function Home() {
return (<div>
<Link href="/login">
Login
</Link>
<br/>
<Link href="/protected-route">
Protected Route
</Link>
</div>)
}
25 changes: 25 additions & 0 deletions nextjs-example/app/protected-route/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {cookies} from "next/headers";
import AuthClient from "@/util/AuthClient";
import {json} from "node:stream/consumers";

async function checkStatus() {

const access_token = cookies().get("access_token")?.value;
// @ts-ignore
const validateToken = await AuthClient.validateAccessToken(access_token);
return validateToken
}

export default async function ProtectedRoute() {
const isTokenValid = await checkStatus();

if (!isTokenValid) {
return (<div>
Please login in.
</div>)
}

return (<div>
This is a Protected Route.
</div>)
}
40 changes: 40 additions & 0 deletions nextjs-example/app/user/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import AuthClient from "@/util/AuthClient";
import {cookies} from "next/headers";


export async function GET(request: Request) {
const url = new URL(request.url);
const queryParams = new URLSearchParams(url.search);

// @ts-ignore
const token : string = queryParams.get('code');


try {
const user: any = await AuthClient.getUser(token);


cookies().set("access_token", user.access_token, {
maxAge: 3600 * 24
});

return new Response(JSON.stringify(user), {status: 200, headers: {'Content-Type': 'application/json'}});

} catch (error) {
try {
// @ts-ignore
const {detail} = JSON.parse(error.message.split('\n').pop());

if (detail === 'Token Expired - token expired') {
return new Response('Token expired', {status: 401});
}
} catch (e) {
console.log('Error parsing response:', e);
}

console.log("LOG:", error);
return new Response('Internal server error', {status: 500});
}


}
4 changes: 4 additions & 0 deletions nextjs-example/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig
Loading

0 comments on commit e89f811

Please sign in to comment.