Skip to content

Commit

Permalink
Add lemmy v0.19 authentication support (#750)
Browse files Browse the repository at this point in the history
Resolves #745
  • Loading branch information
aeharding authored Oct 5, 2023
1 parent a0b52ba commit 9cc9a3e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ app.use(
req.path === "pictrs/image" &&
req.query?.auth
) {
clientReq.setHeader("cookie", `jwt=${req.query.auth}`);
clientReq.setHeader("cookie", `jwt=${req.query.auth}`); // lemmy <=v0.18
clientReq.setHeader("Authorization", `Bearer ${req.query.auth}`); // lemmy >=v0.19
delete req.query.auth;
}
},
Expand Down
7 changes: 1 addition & 6 deletions src/features/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ export default function Login({

try {
await dispatch(
login(
getClient(server ?? customServerHostname),
username,
password,
totp,
),
login(server ?? customServerHostname, username, password, totp),
);
} catch (error) {
if (error === "missing_totp_token") {
Expand Down
23 changes: 15 additions & 8 deletions src/features/auth/authSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PayloadAction, createSelector, createSlice } from "@reduxjs/toolkit";
import { GetSiteResponse, LemmyHttp } from "lemmy-js-client";
import { GetSiteResponse } from "lemmy-js-client";
import { AppDispatch, RootState } from "../../store";
import Cookies from "js-cookie";
import { LemmyJWT, getRemoteHandle } from "../../helpers/lemmy";
Expand Down Expand Up @@ -182,8 +182,10 @@ export const localUserSelector = (state: RootState) =>
state.auth.site?.my_user?.local_user_view.local_user;

export const login =
(client: LemmyHttp, username: string, password: string, totp?: string) =>
(baseUrl: string, username: string, password: string, totp?: string) =>
async (dispatch: AppDispatch) => {
const client = getClient(baseUrl);

const res = await client.login({
username_or_email: username,
password,
Expand All @@ -195,7 +197,9 @@ export const login =
throw new Error("broke");
}

const site = await client.getSite({ auth: res.jwt });
const authenticatedClient = getClient(baseUrl, res.jwt);

const site = await authenticatedClient.getSite({ auth: res.jwt });
const myUser = site.my_user?.local_user_view?.person;

if (!myUser) throw new Error("broke");
Expand Down Expand Up @@ -224,7 +228,7 @@ export const getSite =
const jwtPayload = jwtPayloadSelector(getState());
const instance = jwtPayload?.iss ?? getState().auth.connectedInstance;

const details = await getClient(instance).getSite({
const details = await getClient(instance, jwtSelector(getState())).getSite({
auth: jwtSelector(getState()),
});

Expand Down Expand Up @@ -284,10 +288,13 @@ export const urlSelector = createSelector(
},
);

export const clientSelector = createSelector([urlSelector], (url) => {
// never leak the jwt to the incorrect server
return getClient(url);
});
export const clientSelector = createSelector(
[urlSelector, jwtSelector],
(url, jwt) => {
// never leak the jwt to the incorrect server
return getClient(url, jwt);
},
);

function updateCredentialsStorage(
accounts: CredentialStoragePayload | undefined,
Expand Down
7 changes: 6 additions & 1 deletion src/services/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ function buildProxiedBaseUrl(url: string): string {
return `${location.origin}/api/${url}`;
}

export function getClient(url: string): LemmyHttp {
export function getClient(url: string, jwt?: string): LemmyHttp {
return new LemmyHttp(buildBaseUrl(url), {
// Capacitor http plugin is not compatible with cross-fetch.
// Bind to globalThis or lemmy-js-client will bind incorrectly
fetchFunction: fetch.bind(globalThis),
headers: {
Authorization: jwt ? `Bearer ${jwt}` : undefined,
} as {
[key: string]: string;
},
});
}

Expand Down

0 comments on commit 9cc9a3e

Please sign in to comment.