diff --git a/components/PwaManager.tsx b/components/PwaManager.tsx index 1eae6ce..ceb6577 100644 --- a/components/PwaManager.tsx +++ b/components/PwaManager.tsx @@ -151,7 +151,7 @@ function PwaManager() { return; } - await fetch('/api/notification', { + await fetch('/api/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -219,6 +219,7 @@ function PwaManager() { Телеграм + < ); diff --git a/components/sidebar.tsx b/components/sidebar.tsx index 01533b8..3f2d4c2 100644 --- a/components/sidebar.tsx +++ b/components/sidebar.tsx @@ -163,7 +163,7 @@ function UserSection({ session }) { function SignInButton() { return ( -
signIn()}> +
signIn()}>
); diff --git a/pages/api/auth/apple-signin.ts b/pages/api/auth/apple-signin.ts new file mode 100644 index 0000000..8b152a7 --- /dev/null +++ b/pages/api/auth/apple-signin.ts @@ -0,0 +1,40 @@ +// pages/api/auth/apple.js +import jwt from 'jsonwebtoken'; +import axios from 'axios'; +import fs from 'fs'; +import path from 'path'; + +export default async function handler(req, res) { + if (req.method === 'GET') { + // Generate the client secret + const clientSecret = generateClientSecret(); + const redirectUri = `${req.headers.origin}/api/auth/apple/callback`; + + // Redirect to Apple's authorization page + res.redirect(`https://appleid.apple.com/auth/authorize?response_type=code&client_id=${process.env.APPLE_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=name+email&response_mode=form_post&state=STATE&client_secret=${encodeURIComponent(clientSecret)}`); + } else { + res.status(405).json({ error: 'Method not allowed' }); + } +} + +function generateClientSecret() { + const appleKey = fs.readFileSync(path.resolve('./_deploy/appleKey.p8'), 'utf8'); + const teamID = process.env.APPLE_TEAM_ID || "XC57P9SXDK"; + const keyID = process.env.APPLE_KEY_ID || "TB3V355G5Y"; + const appleAppID = process.env.APPLE_APP_ID || "com.mwitnessing.mwitnessing"; + + // Token expiration + const now = Math.floor(Date.now() / 1000); + const exp = now + 86400 * 180; // 6 months + + const claims = { + iss: teamID, + iat: now, + exp: exp, + aud: 'https://appleid.apple.com', + sub: appleAppID, + }; + + const token = jwt.sign(claims, privateKey, { algorithm: 'ES256', header: { alg: 'ES256', kid: keyId } }); + return token; +} diff --git a/pages/api/notify.ts b/pages/api/notify.ts new file mode 100644 index 0000000..34ebd93 --- /dev/null +++ b/pages/api/notify.ts @@ -0,0 +1,37 @@ + +const webPush = require('web-push') + +webPush.setVapidDetails( + `mailto:${process.env.WEB_PUSH_EMAIL}`, + process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY, + process.env.WEB_PUSH_PRIVATE_KEY +) + +const Notification = (req, res) => { + if (req.method == 'POST') { + const { subscription } = req.body + + webPush + .sendNotification( + subscription, + JSON.stringify({ title: 'Hello Web Push', message: 'Your web push notification is here!' }) + ) + .then(response => { + res.writeHead(response.statusCode, response.headers).end(response.body) + }) + .catch(err => { + if ('statusCode' in err) { + res.writeHead(err.statusCode, err.headers).end(err.body) + } else { + console.error(err) + res.statusCode = 500 + res.end() + } + }) + } else { + res.statusCode = 405 + res.end() + } +} + +export default Notification