From d2740b141ed971c55215ddfa1910a85c7e35b884 Mon Sep 17 00:00:00 2001 From: iwataka Date: Fri, 6 May 2022 23:00:12 +0900 Subject: [PATCH] Modify setup page to show pre-entered consumer key and secret --- server.go | 26 ++++++++++++++++++++++++-- web/src/App.tsx | 30 ++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/server.go b/server.go index 1f45a29..ab4801a 100644 --- a/server.go +++ b/server.go @@ -224,7 +224,8 @@ func setupRouterWithWrapper( r.GET("/api/analysis/image/result", apiWrapper(apiAnalysisImageResultHandler)) r.GET("/api/analysis/image/status", apiWrapper(apiAnalysisImageStatusHandler)) r.GET("/api/auth/status", apiWrapHandler(apiAuthStatus)) - r.POST("/api/auth/credential", apiCredential) + r.POST("/api/auth/credential", postApiCredential) + r.GET("/api/auth/credential", getApiCredential) r.GET("/api/auth/:provider", authHandler) r.GET("/api/auth/callback/:provider", authCallbackHandler) r.GET("/api/config", apiWrapper(apiConfigHandler)) @@ -1142,7 +1143,7 @@ type Credential struct { } `json:"slack"` } -func apiCredential(c *gin.Context) { +func postApiCredential(c *gin.Context) { var body Credential err := c.BindJSON(&body) if err != nil { @@ -1170,6 +1171,27 @@ func apiCredential(c *gin.Context) { c.Status(http.StatusOK) } +func getApiCredential(c *gin.Context) { + twitterCk, twitterCs := "", "" + if twitterApp != nil { + twitterCk, twitterCs = twitterApp.GetCreds() + } + slackCk, slackCs := "", "" + if slackApp != nil { + slackCk, slackCs = slackApp.GetCreds() + } + c.JSON(http.StatusOK, gin.H{ + "twitter": gin.H{ + "consumer_key": twitterCk, + "consumer_secret": twitterCs, + }, + "slack": gin.H{ + "consumer_key": slackCk, + "consumer_secret": slackCs, + }, + }) +} + func apiConfigHandler(c *gin.Context) { user, err := authenticator.GetLoginUser(c.Request) if err != nil { diff --git a/web/src/App.tsx b/web/src/App.tsx index 7d8b16d..5f8b502 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -637,6 +637,24 @@ class Setup extends React.Component { this.updateState = this.updateState.bind(this); } + componentDidMount() { + fetch("/api/auth/credential", { + credentials: "same-origin", + }) + .then((res) => { + if (res.ok) { + res.json().then((data) => { + this.setState({ credential: data }); + }); + } else { + res.text().then((t) => this.setState({ error: t })); + } + }) + .catch((err) => { + this.setState({ error: err }); + }); + } + submit() { fetch("/api/auth/credential", { credentials: "same-origin", @@ -685,9 +703,9 @@ class Setup extends React.Component { Consumer Key { this.updateState("twitter", "consumer_key", e.target.value); }} @@ -698,7 +716,7 @@ class Setup extends React.Component { { this.updateState("twitter", "consumer_secret", e.target.value); }} @@ -713,9 +731,9 @@ class Setup extends React.Component { Consumer Key { this.updateState("slack", "consumer_key", e.target.value); }} @@ -726,7 +744,7 @@ class Setup extends React.Component { { this.updateState("slack", "consumer_secret", e.target.value); }}