Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload: refactor - move osmApiAuthServer #866

Merged
merged 2 commits into from
Jan 5, 2025
Merged
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
6 changes: 2 additions & 4 deletions pages/api/token-login.ts → pages/api/user.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions src/server/osmApiAuthServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fetch from 'isomorphic-unfetch';
import { FetchError } from '../services/helpers';

const API_URL = `https://api.openstreetmap.org`;

const authFetch = async <T = any>(
endpoint: string,
token: string,
): Promise<T> => {
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;
};
2 changes: 1 addition & 1 deletion src/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class FetchError extends Error {
constructor(
public message: string = '',
public code: string,
public data: string,
public data: string = '',
) {
super();
}
Expand Down
5 changes: 1 addition & 4 deletions src/services/osmApiAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ export const loginAndfetchOsmUser = async (): Promise<OsmUser> => {
Cookies.set('osmAccessToken', osmAccessToken, { path: '/', expires: 365 });
Cookies.set('osmUserForSSR', osmUserForSSR, { path: '/', expires: 365 });

await fetch('/api/token-login');

return osmUser;
};

Expand Down Expand Up @@ -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}`;
};

Expand Down
47 changes: 0 additions & 47 deletions src/services/osmApiAuthServer.ts

This file was deleted.

Loading