Skip to content

Commit

Permalink
feat(express-sample): add express sample code (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Aug 11, 2022
1 parent da322e2 commit 301ea8d
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 63 deletions.
38 changes: 38 additions & 0 deletions packages/express-sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@logto/express-sample",
"version": "1.0.0-beta.0",
"license": "MIT",
"private": true,
"scripts": {
"start": "ts-node src/app.ts"
},
"dependencies": {
"@logto/express": "^1.0.0-beta.0",
"cookie-parser": "^1.4.6",
"express": "^4.18.1",
"express-session": "^1.17.3"
},
"devDependencies": {
"@silverhand/eslint-config": "^0.17.0",
"@silverhand/ts-config": "^0.14.0",
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.5",
"@types/node": "^18.6.1",
"eslint": "^8.9.0",
"lint-staged": "^13.0.0",
"postcss": "^8.4.6",
"postcss-modules": "^4.3.0",
"prettier": "^2.5.1",
"stylelint": "^14.8.2",
"ts-node": "^10.9.1",
"typescript": "^4.5.5"
},
"eslintConfig": {
"extends": "@silverhand"
},
"stylelint": {
"extends": "@silverhand/eslint-config/.stylelintrc"
},
"prettier": "@silverhand/eslint-config/.prettierrc"
}
45 changes: 45 additions & 0 deletions packages/express-sample/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import http from 'http';

import cookieParser from 'cookie-parser';
import express, { Request, Response, NextFunction } from 'express';
import session from 'express-session';

import { logtoClient } from './logto';

const requireAuth = async (request: Request, response: Response, next: NextFunction) => {
if (!request.user.isAuthenticated) {
response.redirect('/logto/sign-in');
}

next();
};

const app = express();
app.use(cookieParser());
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 14 * 24 * 60 * 60 } }));
app.use(logtoClient.handleAuthRoutes());
app.use(logtoClient.withLogto());

app.get('/', (request, response) => {
response.setHeader('content-type', 'text/html');
response.end(
`<h1>Hello Logto</h1>
<div><a href="/logto/sign-in">Sign In</a></div>
<div><a href="/logto/sign-out">Sign Out</a></div>
<div><a href="/user">Profile</a></div>
<div><a href="/protected">Protected Resource</a></div>`
);
});

app.get('/user', (request, response) => {
response.json(request.user);
});

app.get('/protected', requireAuth, (request, response) => {
response.end('protected resource');
});

const server = http.createServer(app);
server.listen(3000, () => {
console.log('Sample app listening on http://localhost:3000');
});
8 changes: 8 additions & 0 deletions packages/express-sample/src/logto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import LogtoClient from '@logto/express';

export const logtoClient = new LogtoClient({
appId: 'foo-traditional',
appSecret: 'TXxxky90RxGNFeStfP2xv--ZhsPoz9VGRn5PDbEI1iAACGZp6R_IN0iigujq42V5',
endpoint: 'https://logto.dev',
baseUrl: 'http://localhost:3000',
});
3 changes: 3 additions & 0 deletions packages/express-sample/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@silverhand/ts-config/tsconfig.base"
}
Loading

0 comments on commit 301ea8d

Please sign in to comment.