models: moves from username checks to id checks because users change usernames

This commit is contained in:
@wwwjim 2020-07-22 03:51:40 -07:00
parent 3325f836f8
commit a533b5249b
18 changed files with 114 additions and 63 deletions

View File

@ -163,7 +163,7 @@ export default class ApplicationHeader extends React.Component {
style={{ marginLeft: 12 }}
onClick={() => {}}
size={32}
url={this.props.viewer.photoURL}
url={this.props.viewer.data.photo}
popover={
<System.PopoverNavigation
style={{ right: 0, top: "48px", cursor: "pointer" }}

View File

@ -80,6 +80,7 @@ export default class SidebarFileStorageDeal extends React.Component {
const json = await response.json();
return json;
*/
alert("TODO: Add back storage deals");
};
_handleSubmit = async (e) => {

View File

@ -2,5 +2,12 @@ import createUser from "~/node_common/data/methods/create-user";
import updateUserById from "~/node_common/data/methods/update-user-by-id";
import deleteUserByUsername from "~/node_common/data/methods/delete-user-by-username";
import getUserByUsername from "~/node_common/data/methods/get-user-by-username";
import getUserById from "~/node_common/data/methods/get-user-by-id";
export { createUser, updateUserById, deleteUserByUsername, getUserByUsername };
export {
createUser,
updateUserById,
deleteUserByUsername,
getUserByUsername,
getUserById,
};

View File

@ -0,0 +1,29 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id }) => {
return await runQuery({
label: "GET_USER_BY_ID",
queryFn: async (DB) => {
const query = await DB.select("*")
.from("users")
.where({ id })
.first();
if (!query || query.error) {
return null;
}
if (query.id) {
return query;
}
return null;
},
errorFn: async (e) => {
return {
error: "GET_USER_BY_ID",
source: e,
};
},
});
};

View File

@ -1,16 +1,22 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id, data }) => {
export default async ({ id, data, username }) => {
const updateObject = {};
if (data) {
updateObject.data = data;
}
if (username) {
updateObject.username = username;
}
return await runQuery({
label: "UPDATE_USER_BY_ID",
queryFn: async (DB) => {
const response = await DB.from("users")
.where("id", id)
.update({
data: {
...data,
},
})
.update(updateObject)
.returning("*");
const index = response ? response.pop() : null;

View File

@ -51,8 +51,8 @@ export const RequireCookieAuthentication = async (req, res, next) => {
try {
const decoded = JWT.verify(token, Environment.JWT_SECRET);
const user = await Data.getUserByUsername({
username: decoded.username,
const user = await Data.getUserById({
id: decoded.id,
});
if (!user || user.error) {

View File

@ -3,9 +3,9 @@ import * as Data from "~/node_common/data";
import PG from "~/node_common/powergate";
export const getViewer = async ({ username }) => {
const user = await Data.getUserByUsername({
username,
export const getViewer = async ({ id }) => {
const user = await Data.getUserById({
id,
});
if (!user) {

View File

@ -16,8 +16,8 @@ const TEXTILE_KEY_INFO = {
secret: Environment.TEXTILE_HUB_SECRET,
};
export const getUserFromCookie = (req) => {
let username;
export const getIdFromCookie = (req) => {
let id;
if (!Strings.isEmpty(req.headers.cookie)) {
const token = req.headers.cookie.replace(
/(?:(?:^|.*;\s*)WEB_SERVICE_SESSION_KEY\s*\=\s*([^;]*).*$)|^.*$/,
@ -27,14 +27,14 @@ export const getUserFromCookie = (req) => {
if (!Strings.isEmpty(token)) {
try {
const decoded = JWT.verify(token, Environment.JWT_SECRET);
username = decoded.username;
id = decoded.id;
} catch (e) {
console.log(e);
}
}
}
return username;
return id;
};
export const parseAuthHeader = (value) => {

View File

@ -11,15 +11,15 @@ export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const username = Utilities.getUserFromCookie(req);
if (!username) {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res
.status(500)
.json({ decorator: "SERVER_SEND_FILECOIN", error: true });
}
const user = await Data.getUserByUsername({
username,
const user = await Data.getUserById({
id,
});
if (!user) {

View File

@ -45,9 +45,9 @@ export default async (req, res) => {
};
// TODO(jim): Send this file to buckets.
const username = Utilities.getUserFromCookie(req);
const user = await Data.getUserByUsername({
username,
const id = Utilities.getIdFromCookie(req);
const user = await Data.getUserById({
id,
});
const {

View File

@ -9,14 +9,14 @@ export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const username = Utilities.getUserFromCookie(req);
if (!username) {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res
.status(500)
.json({ decorator: "SERVER_USER_DELETE", error: true });
}
const data = await Models.getViewer({ username });
const data = await Models.getViewer({ id });
return res
.status(200)

View File

@ -65,7 +65,7 @@ export default async (req, res) => {
}
const token = JWT.sign(
{ user: user.id, username: user.username },
{ id: user.id, username: user.username },
Environment.JWT_SECRET
);

View File

@ -86,6 +86,6 @@ export default async (req, res) => {
return res.status(200).json({
decorator: "SERVER_USER_CREATE",
user: { username: user.username },
user: { username: user.username, id: user.id },
});
};

View File

@ -21,15 +21,15 @@ export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const username = Utilities.getUserFromCookie(req);
if (!username) {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res
.status(500)
.json({ decorator: "SERVER_USER_DELETE", error: true });
}
const user = await Data.getUserByUsername({
username,
const user = await Data.getUserById({
id,
});
if (!user) {

View File

@ -13,15 +13,15 @@ export default async (req, res) => {
initCORS(req, res);
initAuth(req, res);
const username = Utilities.getUserFromCookie(req);
if (!username) {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res
.status(500)
.json({ decorator: "SERVER_USER_UPDATE", error: true });
}
const user = await Data.getUserByUsername({
username,
const user = await Data.getUserById({
id,
});
if (!user) {
@ -43,6 +43,19 @@ export default async (req, res) => {
});
}
if (req.body.username) {
const existing = await Data.getUserByUsername({
username: req.body.username,
});
if (!existing) {
await Data.updateUserById({
id: user.id,
username: req.body.username,
});
}
}
// TODO(jim): POWERGATE_ISSUE 0.2.0
// Should work when our hosted Powergate works.
if (req.body.type === "SET_DEFAULT_STORAGE_CONFIG") {

View File

@ -453,6 +453,7 @@ export default class ApplicationPage extends React.Component {
onAction: this._handleAction,
onBack: this._handleBack,
onForward: this._handleForward,
onRehydrate: this.rehydrate,
});
let sidebarElement;
@ -465,6 +466,7 @@ export default class ApplicationPage extends React.Component {
onSubmit: this._handleSubmit,
onCancel: this._handleCancel,
onSetFile: this._handleSetFile,
onRehydrate: this.rehydrate,
});
}

View File

@ -28,9 +28,6 @@ export default class SceneEditAccount extends React.Component {
state = { username: this.props.viewer.username, deleting: false };
_handleUpload = async (e) => {
// TODO(jim):
// Rewrite
/*
e.persist();
let file = e.target.files[0];
@ -49,26 +46,22 @@ export default class SceneEditAccount extends React.Component {
body: data,
};
await fetch(`/_/upload/avatar`, options);
*/
const response = await fetch(`/api/data/${file.name}`, options);
const json = await response.json();
await Actions.updateViewer({
data: { photo: `https://hub.textile.io${json.data.ipfs}` },
});
this.props.onRehydrate();
};
_handleSave = async (e) => {
// TODO(jim):
// Rewrite
/*
const options = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify({ local: { name: this.state.name } }),
};
await Actions.updateViewer({
username: this.state.username,
});
await fetch(`/_/local-settings`, options);
*/
this.props.onRehydrate();
};
_handleDelete = async (e) => {
@ -101,7 +94,7 @@ export default class SceneEditAccount extends React.Component {
<Avatar
style={{ marginTop: 24 }}
size={256}
url={this.props.viewer.photo}
url={this.props.viewer.data.photo}
/>
<div style={{ marginTop: 24 }}>

View File

@ -26,12 +26,12 @@ app.prepare().then(async () => {
server.use("/public", express.static("public"));
server.get("/application", async (req, res) => {
const username = Utilities.getUserFromCookie(req);
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (username) {
if (id) {
viewer = await Models.getViewer({
username,
id,
});
}
@ -43,12 +43,12 @@ app.prepare().then(async () => {
});
server.get("/@:username", async (req, res) => {
const username = Utilities.getUserFromCookie(req);
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (username) {
if (id) {
viewer = await Models.getViewer({
username,
id,
});
}