diff --git a/pages/api/token-login.ts b/pages/api/user.ts similarity index 54% rename from pages/api/token-login.ts rename to pages/api/user.ts index ba97a0c5b..cc273c359 100644 --- a/pages/api/token-login.ts +++ b/pages/api/user.ts @@ -1,11 +1,9 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { serverFetchOsmUser } from '../../src/services/osmApiAuthServer'; +import { serverFetchOsmUser } from '../../src/server/osmApiAuthServer'; -// TODO upgrade Nextjs and use export async function POST(request: NextRequest) { export default async (req: NextApiRequest, res: NextApiResponse) => { try { - const { osmAccessToken } = req.cookies; - const user = await serverFetchOsmUser({ osmAccessToken }); + const user = await serverFetchOsmUser(req.cookies.osmAccessToken); res.status(200).json({ user }); } catch (err) { diff --git a/src/server/osmApiAuthServer.ts b/src/server/osmApiAuthServer.ts new file mode 100644 index 000000000..89bd8da8f --- /dev/null +++ b/src/server/osmApiAuthServer.ts @@ -0,0 +1,41 @@ +import fetch from 'isomorphic-unfetch'; +import { FetchError } from '../services/helpers'; + +const API_URL = `https://api.openstreetmap.org`; + +const authFetch = async ( + endpoint: string, + token: string, +): Promise => { + if (!token) { + throw new FetchError('No OSM access token', '401'); + } + + const response = await fetch(`${API_URL}${endpoint}`, { + headers: { + 'User-Agent': `osmapp ${process.env.osmappVersion} (SSR; https://osmapp.org/)`, + Authorization: `Bearer ${token}`, + }, + }); + + if (!response.ok || response.status < 200 || response.status >= 300) { + const data = await response.text(); + throw new FetchError( + `${response.status} ${response.statusText}`, + `${response.status}`, + data, + ); + } + + return response.json(); +}; + +export type ServerOsmUser = { id: number; username: string }; + +export const serverFetchOsmUser = async (token: string) => { + const { user } = await authFetch('/api/0.6/user/details.json', token); + return { + id: user.id, + username: user.display_name, + } as ServerOsmUser; +}; diff --git a/src/services/helpers.ts b/src/services/helpers.ts index 2db35d806..819b16eca 100644 --- a/src/services/helpers.ts +++ b/src/services/helpers.ts @@ -191,7 +191,7 @@ export class FetchError extends Error { constructor( public message: string = '', public code: string, - public data: string, + public data: string = '', ) { super(); } diff --git a/src/services/osmApiAuth.ts b/src/services/osmApiAuth.ts index 6c5888adf..19b10caee 100644 --- a/src/services/osmApiAuth.ts +++ b/src/services/osmApiAuth.ts @@ -79,8 +79,6 @@ export const loginAndfetchOsmUser = async (): Promise => { Cookies.set('osmAccessToken', osmAccessToken, { path: '/', expires: 365 }); Cookies.set('osmUserForSSR', osmUserForSSR, { path: '/', expires: 365 }); - await fetch('/api/token-login'); - return osmUser; }; @@ -197,8 +195,7 @@ const getItemOrLastHistoric = async ( const getDescription = (toBeDeleted: boolean, feature: Feature) => { const undelete = feature.deleted; const action = undelete ? 'Undeleted' : toBeDeleted ? 'Deleted' : 'Edited'; - const { subclass } = feature.properties; - const name = feature.tags.name || subclass || getUrlOsmId(feature.osmMeta); + const name = getLabel(feature) || getUrlOsmId(feature.osmMeta); return `${action} ${name}`; }; diff --git a/src/services/osmApiAuthServer.ts b/src/services/osmApiAuthServer.ts deleted file mode 100644 index 6454e30c1..000000000 --- a/src/services/osmApiAuthServer.ts +++ /dev/null @@ -1,47 +0,0 @@ -import fetch from 'isomorphic-unfetch'; -import { FetchError } from './helpers'; - -interface OsmAuthFetchOpts extends RequestInit { - osmAccessToken: string; -} - -const osmAuthFetch = async ( - endpoint: string, - options: OsmAuthFetchOpts, -): Promise => { - const { osmAccessToken, ...restOptions } = options; - if (!osmAccessToken) throw new Error('No access token'); - - const url = `https://api.openstreetmap.org${endpoint}`; - const headers = { - 'User-Agent': 'osmapp (SSR; https://osmapp.org/)', - 'Content-Type': 'application/x-www-form-urlencoded', - Authorization: `Bearer ${osmAccessToken}`, - }; - - const response = await fetch(url, { - ...restOptions, - headers, - }); - - if (!response.ok || response.status < 200 || response.status >= 300) { - const data = await response.text(); - throw new FetchError( - `${response.status} ${response.statusText}`, - `${response.status}`, - data, - ); - } - - return response.json(); -}; - -export const serverFetchOsmUser = async ( - options: OsmAuthFetchOpts, -): Promise<{ id: number; username: string }> => { - const { user } = await osmAuthFetch('/api/0.6/user/details.json', options); - return { - id: user.id, - username: user.display_name, - }; -};