Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions node/express/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";

//authorization
import passport from "./config/passport";
import session from "express-session";

// route
import { router as chatRouter } from "./routes/chat";
import { router as apiBoardRouter } from "./routes/api/board";
import { router as loginRouter } from "./routes/login";
import { router as callbackRouter } from "./routes/callback";
import { router as apiCallbackRouter } from "./routes/api/callback";
import { router as planRouter } from "./routes/plan";
import { router as tenantRouter } from "./routes/tenant";

//middleware
import { AuthMiddleware } from "saasus-sdk";

import cors from "cors";

if (process.env.NODE_ENV !== "production") {
Expand All @@ -41,25 +41,22 @@ app.use(
);

app.use(
session({
secret: "secret",
resave: false,
saveUninitialized: false,
})
["/chat", "/api/board", "/api/post", "/api/plan", "/api/tenant"],
AuthMiddleware
);
app.use(passport.initialize());
app.use(passport.session());

app.use("/chat", chatRouter);

app.use(["/api/board", "/api/post"], apiBoardRouter);

app.use("/callback", callbackRouter);

app.use("/api/callback", apiCallbackRouter);

app.use("/api/plan", planRouter);

app.use("/api/tenant", tenantRouter);

app.use("/login", loginRouter);

app.use((req: Request, res: Response, next: NextFunction) => {
next(createError(404));
});
Expand Down
47 changes: 39 additions & 8 deletions node/express/api/controllers/api/board.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Request, Response } from "express";
import { UpdateMeteringUnitTimestampCountNowParam } from "saasus-sdk/dist/generated/Pricing";
const db = require("../../models/index");
import { TENANT_ID } from "../tenant";
import { findUpperCountByMeteringUnitName, PricingClient } from "saasus-sdk";

const getBoard = async (req: Request, res: Response) => {
try {
const messages = await db.Messages.findAll({
where: {
tenant_id: TENANT_ID,
tenant_id: req.userInfo?.tenants[0].id,
},
});
return res.json(messages);
Expand All @@ -20,13 +21,43 @@ const getBoard = async (req: Request, res: Response) => {

const post = async (req: Request, res: Response) => {
const mes = req.body.message;
const userName = "テストユーザー";
const tenantId = req.userInfo?.tenants[0].id || "";
const planId = req.userInfo?.tenants[0].plan_id || "";
const userName =
req.userInfo?.tenants[0].user_attribute.username || "テストユーザー";
try {
await db.Messages.create({
tenant_id: TENANT_ID,
user_id: userName,
message: mes,
});
const pricingClient = new PricingClient();
const pricingPlan = await pricingClient.pricingPlansApi.getPricingPlan(
planId
);
const meteringUnitName = "comment_count";
const meteringUnitCountData =
await pricingClient.meteringApi.getMeteringUnitDateCountByTenantIdAndUnitNameToday(
tenantId,
meteringUnitName
);
const upper = findUpperCountByMeteringUnitName(
pricingPlan.data,
meteringUnitName
);
if (meteringUnitCountData.data.count < upper || upper === 0) {
await db.Messages.create({
tenant_id: tenantId,
user_id: userName,
message: mes,
});
let param: UpdateMeteringUnitTimestampCountNowParam = {
method: "add",
count: 1,
};
const res =
await pricingClient.meteringApi.updateMeteringUnitTimestampCountNow(
tenantId,
meteringUnitName,
param
);
}
return res.status(201).send();
} catch (error) {
console.error(error);
}
Expand Down
53 changes: 41 additions & 12 deletions node/express/api/controllers/chat.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { SessionUser } from "config/passport";
import { TENANT_ID, TENANT_NAME } from "./tenant";
import { Request, Response } from "express";
import { PLANS } from "./plan";
const db = require("./../models/index");
import { UpdateMeteringUnitTimestampCountNowParam } from "saasus-sdk/dist/generated/Pricing";
const db = require("../models/index");
import { findUpperCountByMeteringUnitName, PricingClient } from "saasus-sdk";
import { PLANS } from "../controllers/plan";
import { TENANT_NAME } from "../controllers/tenant";

const getChats = async (req: Request, res: Response) => {
try {
const messages = await db.Messages.findAll({
where: {
tenant_id: TENANT_ID,
tenant_id: req.userInfo?.tenants[0].id,
},
});
res.render("chat", {
Expand All @@ -24,14 +25,42 @@ const getChats = async (req: Request, res: Response) => {

const postChats = async (req: Request, res: Response) => {
const mes = req.body.message;
const sessionUser: SessionUser = req.user || {};
const userName = sessionUser.name;
const tenantId = req.userInfo?.tenants[0].id || "";
const planId = req.userInfo?.tenants[0].plan_id || "";
const userName =
req.userInfo?.tenants[0].user_attribute.username || "テストユーザー";
try {
await db.Messages.create({
tenant_id: TENANT_ID,
user_id: userName,
message: mes,
});
const pricingClient = new PricingClient();
const pricingPlan = await pricingClient.pricingPlansApi.getPricingPlan(
planId
);
const meteringUnitName = "comment_count";
const meteringUnitCountData =
await pricingClient.meteringApi.getMeteringUnitDateCountByTenantIdAndUnitNameToday(
tenantId,
meteringUnitName
);
const upper = findUpperCountByMeteringUnitName(
pricingPlan.data,
meteringUnitName
);
if (meteringUnitCountData.data.count < upper || upper === 0) {
await db.Messages.create({
tenant_id: tenantId,
user_id: userName,
message: mes,
});
let param: UpdateMeteringUnitTimestampCountNowParam = {
method: "add",
count: 1,
};
const res =
await pricingClient.meteringApi.updateMeteringUnitTimestampCountNow(
tenantId,
meteringUnitName,
param
);
}
} catch (error) {
console.error(error);
}
Expand Down
27 changes: 27 additions & 0 deletions node/express/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/express/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"passport-local": "^1.0.0",
"pg": "^8.8.0",
"pg-hstore": "^2.3.4",
"saasus-sdk": "^1.2.0",
"sequelize": "^6.25.5"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions node/express/api/routes/api/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import express from "express";
const router = express.Router();
import { CallbackRouteFunction } from "saasus-sdk";

router.get("/", CallbackRouteFunction);

export { router };
7 changes: 7 additions & 0 deletions node/express/api/routes/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import express from "express";
const router = express.Router();
import { CallbackRouteFunction } from "saasus-sdk";

router.get("/", CallbackRouteFunction);

export { router };
12 changes: 1 addition & 11 deletions node/express/api/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ import express, { Request, Response, NextFunction } from "express";
const router = express.Router();
import { getChats, postChats } from "../controllers/chat";

router.get(
"/",
(req: Request, res: Response, next: NextFunction) => {
if (req.isAuthenticated()) {
next();
} else {
res.redirect(302, "/login");
}
},
getChats
);
router.get("/", getChats);

router.post("/", postChats);

Expand Down
15 changes: 15 additions & 0 deletions node/express/api/views/callback.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<title>Auth Callback</title>
</head>

<body>
<script>
location.href = "/chat";
</script>
</body>
</html>
33 changes: 29 additions & 4 deletions node/express/front/src/pages/board/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ import axios from '@/lib/axios'

const Board = () => {
const { mutate } = useSWRConfig()
const fetcher = (url: string) => axios.get(url).then((res) => res.data)
const fetcher = (url: string) => {
// Local StorageからJWTを取得し、Bearer tokenとしてヘッダにつけてAPIコールする
const token = localStorage.getItem('SaaSusIdToken')
if (!token) return ''
return axios
.get(url, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((res) => res.data)
}
const { data: tenant_name, error: tenant_error } = useSWR(
`/api/tenant`,
fetcher
)
const { data: messages, error } = useSWR(`/api/board`, fetcher, {
refreshInterval: 3000,
refreshInterval: 5000,
})
if (
error &&
error.response &&
error.response.data &&
error.response.data.redirect_url
) {
location.replace(error.response.data.redirect_url)
}
if (error || tenant_error) return <div>failed to load</div>
if (!messages || !tenant_name) return <div>loading...</div>

Expand All @@ -22,7 +41,13 @@ const Board = () => {
// 再検証をせずに直ちにローカルデータを更新
mutate('/api/board', [...messages, formValue], false)
// 更新するために API にリクエストを送信
await axios.post('/api/post', formValue)
// Local StorageからJWTを取得し、Bearer tokenとしてヘッダにつけてAPIコールする
const token = localStorage.getItem('SaaSusIdToken')
await axios.post('/api/post', formValue, {
headers: {
Authorization: `Bearer ${token}`,
},
})
}

return (
Expand All @@ -36,4 +61,4 @@ const Board = () => {
)
}

export default Board
export default Board
40 changes: 40 additions & 0 deletions node/express/front/src/pages/callback/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Container from '@mui/material/Container'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
import axios from '@/lib/axios'

const Callback = () => {
const router = useRouter()
const query = router.query
const code = query.code as string

const fetchAuthCredentials = async () => {
try {
const res = await axios.get(`/api/callback?code=${code}`)
// 渡ってきたJWTをLocal Storageに保存する
const idToken = res.data.id_token as string
localStorage.setItem('SaaSusIdToken', idToken)
router.replace('/board')
} catch (error: any) {
if (
error.response &&
error.response.data &&
error.response.data.redirect_url
) {
location.replace(error.response.data.redirect_url)
}
}
}

useEffect(() => {
if (router.isReady) {
if (code) {
fetchAuthCredentials()
}
}
}, [query, router])

return <Container disableGutters></Container>
}

export default Callback