This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: proxy login through the server (#10)
* fix: make path type not nullable * chore: proxy login through the server Co-authored-by: Samer Buna <samerbuna@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
175 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import history from "store/history" | ||
import { history } from "store" | ||
|
||
type Props = { | ||
to: RoutePath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import "cross-fetch/polyfill" // The URQL client depends on fetch | ||
import express from "express" | ||
import { gql, createClient } from "@urql/core" | ||
|
||
import config from "./config" | ||
|
||
const apiRouter = express.Router({ caseSensitive: true }) | ||
|
||
const client = (req: Express.Request) => | ||
createClient({ | ||
url: config.graphqlUri, | ||
fetchOptions: () => { | ||
const token = req.session?.authToken | ||
return { | ||
headers: { authorization: token ? `Bearer ${token}` : "" }, | ||
} | ||
}, | ||
}) | ||
|
||
const MUTATION_USER_LOGIN = gql` | ||
mutation userLogin($input: UserLoginInput!) { | ||
userLogin(input: $input) { | ||
errors { | ||
message | ||
} | ||
authToken | ||
} | ||
} | ||
` | ||
|
||
apiRouter.post("/login", async (req, res) => { | ||
try { | ||
const { phoneNumber, authCode } = req.body | ||
|
||
if (!phoneNumber || !authCode) { | ||
throw new Error("INVALID_LOGIN_REQUEST") | ||
} | ||
|
||
const { error, data } = await client(req) | ||
.mutation(MUTATION_USER_LOGIN, { | ||
input: { phone: phoneNumber, code: authCode }, | ||
}) | ||
.toPromise() | ||
|
||
if (error || data?.userLogin?.errors?.length > 0 || !data?.userLogin?.authToken) { | ||
throw new Error(data?.userLogin?.errors?.[0].message || "SOMETHING_WENT_WRONG") | ||
} | ||
|
||
const authToken = data?.userLogin?.authToken | ||
|
||
req.session = req.session || {} | ||
req.session.authToken = authToken | ||
|
||
return res.send({ authToken }) | ||
} catch (err) { | ||
console.error(err) | ||
return res | ||
.status(500) | ||
.send({ error: err instanceof Error ? err.message : "SOMETHING_WENT_WRONG" }) | ||
} | ||
}) | ||
|
||
apiRouter.post("/logout", async (req, res) => { | ||
req.session = req.session || {} | ||
req.session.authToken = null | ||
return res.send({ authToken: null }) | ||
}) | ||
|
||
export default apiRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import express from "express" | ||
|
||
import { serverRenderer } from "renderers/server" | ||
import { SupportedRoutes } from "./routes" | ||
|
||
const ssrRouter = express.Router({ caseSensitive: true }) | ||
|
||
ssrRouter.get("/*", async (req, res) => { | ||
try { | ||
const routePath = req.path | ||
const checkedRoutePath = SupportedRoutes.find( | ||
(supportedRoute) => supportedRoute === routePath, | ||
) | ||
if (!checkedRoutePath) { | ||
return res.status(404) | ||
} | ||
const vars = await serverRenderer({ | ||
path: checkedRoutePath, | ||
authToken: req.session?.authToken, | ||
}) | ||
return res.render("index", vars) | ||
} catch (err) { | ||
console.error(err) | ||
return res.status(500).send("Server error") | ||
} | ||
}) | ||
|
||
export default ssrRouter |
Oops, something went wrong.