Fix site setup and login. Fixes #699 (#702)

This commit is contained in:
Dessalines 2022-06-27 16:50:47 -04:00 committed by GitHub
parent 9b8377760a
commit 130748a0c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 55 deletions

View file

@ -22,12 +22,10 @@ import {
amAdmin,
auth,
donateLemmyUrl,
getLanguages,
isBrowser,
notifyComment,
notifyPrivateMessage,
numToSI,
setTheme,
showAvatars,
toast,
wsClient,
@ -90,18 +88,13 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
auth: auth().unwrap(),
})
);
this.fetchUnreads();
if (this.props.siteRes.site_view.isSome()) {
this.fetchUnreads();
}
}
this.userSub = UserService.Instance.jwtSub.subscribe(res => {
// A login
if (res.isSome()) {
this.requestNotificationPermission();
WebSocketService.Instance.send(
wsClient.getSite({ auth: res.map(r => r.jwt) })
);
}
});
this.requestNotificationPermission();
// Subscribe to unread count changes
this.unreadInboxCountSub =
@ -628,18 +621,6 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
this.state.unreadApplicationCount = data.registration_applications;
this.setState(this.state);
this.sendApplicationUnread();
} else if (op == UserOperation.GetSite) {
// This is only called on a successful login
let data = wsJsonToRes<GetSiteResponse>(msg, GetSiteResponse);
UserService.Instance.myUserInfo = data.my_user;
UserService.Instance.myUserInfo.match({
some: mui => {
setTheme(mui.local_user_view.local_user.theme);
i18n.changeLanguage(getLanguages()[0]);
this.setState(this.state);
},
none: void 0,
});
} else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);

View file

@ -85,7 +85,7 @@ export class CommunityForm extends Component<
posting_restricted_to_mods: Some(
cv.community.posting_restricted_to_mods
),
auth: auth().unwrap(),
auth: undefined,
});
},
none: void 0,
@ -293,6 +293,7 @@ export class CommunityForm extends Component<
event.preventDefault();
i.state.loading = true;
let cForm = i.state.communityForm;
cForm.auth = auth().unwrap();
i.props.community_view.match({
some: cv => {
@ -304,7 +305,7 @@ export class CommunityForm extends Component<
banner: cForm.banner,
nsfw: cForm.nsfw,
posting_restricted_to_mods: cForm.posting_restricted_to_mods,
auth: auth().unwrap(),
auth: cForm.auth,
});
WebSocketService.Instance.send(wsClient.editCommunity(form));

View file

@ -123,7 +123,10 @@ export class Home extends Component<any, HomeState> {
listingType: getListingTypeFromProps(
this.props,
ListingType[
this.isoData.site_res.site_view.unwrap().site.default_post_listing_type
this.isoData.site_res.site_view.match({
some: type_ => type_.site.default_post_listing_type,
none: ListingType.Local,
})
]
),
dataType: getDataTypeFromProps(this.props),
@ -186,7 +189,7 @@ export class Home extends Component<any, HomeState> {
componentDidMount() {
// This means it hasn't been set up yet
if (!this.state.siteRes.site_view) {
if (this.state.siteRes.site_view.isNone()) {
this.context.router.history.push("/setup");
}
setupTippy();

View file

@ -13,7 +13,6 @@ import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import {
auth,
isBrowser,
setIsoData,
toast,
@ -198,13 +197,6 @@ export class Login extends Component<any, State> {
this.state = this.emptyState;
this.setState(this.state);
UserService.Instance.login(data);
WebSocketService.Instance.send(
wsClient.userJoin({
auth: auth().unwrap(),
})
);
toast(i18n.t("logged_in"));
this.props.history.push("/");
} else if (op == UserOperation.PasswordReset) {
toast(i18n.t("reset_password_mail_sent"));
} else if (op == UserOperation.GetSite) {

View file

@ -39,7 +39,7 @@ export class Setup extends Component<any, State> {
honeypot: None,
answer: None,
}),
doneRegisteringUser: false,
doneRegisteringUser: UserService.Instance.myUserInfo.isSome(),
userLoading: false,
};
@ -206,7 +206,6 @@ export class Setup extends Component<any, State> {
} else if (op == UserOperation.Register) {
let data = wsJsonToRes<LoginResponse>(msg, LoginResponse);
this.state.userLoading = false;
this.state.doneRegisteringUser = true;
UserService.Instance.login(data);
this.setState(this.state);
} else if (op == UserOperation.CreateSite) {

View file

@ -19,7 +19,6 @@ import { Subscription } from "rxjs";
import { i18n } from "../../i18next";
import { UserService, WebSocketService } from "../../services";
import {
auth,
isBrowser,
joinLemmyUrl,
mdToHtml,
@ -537,13 +536,8 @@ export class Signup extends Component<any, State> {
this.state = this.emptyState;
this.setState(this.state);
// Only log them in if a jwt was set
if (data.jwt) {
if (data.jwt.isSome()) {
UserService.Instance.login(data);
WebSocketService.Instance.send(
wsClient.userJoin({
auth: auth().unwrap(),
})
);
this.props.history.push("/communities");
} else {
if (data.verify_email_sent) {

View file

@ -3,7 +3,7 @@ import { Err, None, Ok, Option, Result, Some } from "@sniptt/monads";
import IsomorphicCookie from "isomorphic-cookie";
import jwt_decode from "jwt-decode";
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
import { BehaviorSubject, Subject } from "rxjs";
import { BehaviorSubject } from "rxjs";
import { isHttps } from "../env";
import { i18n } from "../i18next";
import { isBrowser, toast } from "../utils";
@ -23,7 +23,6 @@ export class UserService {
private static _instance: UserService;
public myUserInfo: Option<MyUserInfo> = None;
public jwtInfo: Option<JwtInfo> = None;
public jwtSub: Subject<Option<JwtInfo>> = new Subject<Option<JwtInfo>>();
public unreadInboxCountSub: BehaviorSubject<number> =
new BehaviorSubject<number>(0);
public unreadReportCountSub: BehaviorSubject<number> =
@ -38,19 +37,23 @@ export class UserService {
public login(res: LoginResponse) {
let expires = new Date();
expires.setDate(expires.getDate() + 365);
IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps });
console.log("jwt cookie set");
this.setJwtInfo();
res.jwt.match({
some: jwt => {
toast(i18n.t("logged_in"));
IsomorphicCookie.save("jwt", jwt, { expires, secure: isHttps });
this.setJwtInfo();
location.reload();
},
none: void 0,
});
}
public logout() {
this.jwtInfo = None;
this.myUserInfo = None;
this.jwtSub.next(this.jwtInfo);
IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason
document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.host;
location.reload(); // TODO may not be necessary anymore
console.log("Logged out.");
location.reload();
}
public auth(throwErr = true): Result<string, string> {
@ -74,7 +77,6 @@ export class UserService {
if (jwt) {
let jwtInfo: JwtInfo = { jwt, claims: jwt_decode(jwt) };
this.jwtInfo = Some(jwtInfo);
this.jwtSub.next(this.jwtInfo);
}
}

View file

@ -1221,10 +1221,11 @@ export function setIsoData<Type1, Type2, Type3, Type4, Type5>(
if (routeData[4]) {
routeDataOut[4] = convertWindowJson(cls5, routeData[4]);
}
let site_res = convertWindowJson(GetSiteResponse, json.site_res);
let isoData: IsoData = {
path: json.path,
site_res: convertWindowJson(GetSiteResponse, json.site_res),
site_res,
routeData: routeDataOut,
};
return isoData;