https://github.com/masa-finance/masa-express
Masa Express
https://github.com/masa-finance/masa-express
cryptography express library session
Last synced: 3 months ago
JSON representation
Masa Express
- Host: GitHub
- URL: https://github.com/masa-finance/masa-express
- Owner: masa-finance
- License: mit
- Created: 2023-04-14T15:32:08.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-20T16:46:32.000Z (over 1 year ago)
- Last Synced: 2025-07-29T05:54:59.616Z (11 months ago)
- Topics: cryptography, express, library, session
- Language: TypeScript
- Homepage: https://masa.finance
- Size: 1.21 MB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
* [Installation](#installation)
* [Usage](#usage)
## Installation
yarn
`yarn add @masa-finance/masa-express --save`
npm
`npm i @masa-finance/masa-express --save`
## Usage
A full example can be found in the [Masa Express Boilerplate](https://github.com/masa-finance/masa-express-boilerplate) repo.
```typescript
import express, {
Express,
RequestHandler,
Router,
} from "express";
import {
MasaSessionMiddleware,
MasaSessionRouter,
sessionCheckHandler,
} from "@masa-finance/masa-express";
import {
CreateSoulNameResult,
SoulNameErrorCodes,
} from "@masa-finance/masa-sdk";
import cors
from "cors";
const app: Express = express();
app.use(express.json());
// your session name
const sessionName = "my_fancy_session_name";
// never give this to someone!
const secret = "top_secret_1337";
// 30 days session expiration time
const ttl = 30 * 24 * 60 * 60;
// production, dev or undefined (will fall back to dev then)
const environment = "dev";
// the domain your session should be valid on
const domain = ".vitalik.org";
// custom namespace generated using: https://www.uuidtools.com/generate/v4
const sessionNamespace = "01bbc88d-3cd2-465f-8687-e0ea5e8b1231";
const sessionMiddleware: RequestHandler = MasaSessionMiddleware({
sessionName,
secret,
domain,
ttl,
environment,
});
app.use(
cors({
origin: domain,
credentials: true,
})
);
// session related
app.use(
"/session",
MasaSessionRouter({
sessionMiddleware,
sessionName,
sessionNamespace,
})
);
export const soulNameRouter: Router = express.Router();
soulNameRouter.use(sessionMiddleware);
soulNameRouter.use(sessionCheckHandler as never);
soulNameRouter.post(
"/soul-name/store",
(request: Request, response: Response) => {
const result: CreateSoulNameResult = {
success: false,
message: "Hello world!",
errorCode: SoulNameErrorCodes.UnknownError,
};
console.log(result);
response.json(result);
}
);
app.use(soulNameRouter);
const port: number = 4000; // use whatever port you need
app.listen(port, () => {
console.log(`Express app listening at 'http://localhost:${port}'`);
});
```