Skip to content

Commit

Permalink
Add redirect after login (#98)
Browse files Browse the repository at this point in the history
* impl

* typo
  • Loading branch information
Daniel-Anker-Hermansen authored Feb 5, 2024
1 parent 3577260 commit 05432ee
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 12 deletions.
12 changes: 10 additions & 2 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,17 @@ function App() {
useEffect(getStatisticsList, []);

const flatLinks = [...links.flatMap((x) => (x.subItems ? x.subItems : [x]))];

// Redirect if query parameter is set
let params = new URLSearchParams(window.location.search);
let redirect = params.get("redirect");
if (redirect) {
window.location.href = atob(redirect);
}

return (
<BrowserRouter>
// Only render the page if we are not being redirected
return redirect ? <BrowserRouter></BrowserRouter> :
(<BrowserRouter>
<div id="page-container">
<Topbar links={links} statisticsGroups={statisticsList?.list} />
<div id="content-wrapper">
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/api/statistics.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export class StatisticsApi {
this.queryDatabaseEndpoint = "/database/query";
}

getWcaAuthenticationUrl = (frontendHost: string) =>
getWcaAuthenticationUrl = (frontendHost: string, redirect: string) =>
Axios.get<AuthenticationResponse>(this.BASE_URL + "/wca/authentication", {
params: { frontendHost },
params: { frontendHost, redirect },
});

getUserInfo = () => Axios.get<UserInfo>(this.BASE_URL + "/wca/user");
Expand Down
4 changes: 3 additions & 1 deletion client/src/main/api/wca.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export class WcaApi {
// We get the url from the server
statisticsApi
.getWcaAuthenticationUrl(
window.location.protocol + "//" + window.location.host
window.location.protocol + "//" + window.location.host,
// Redirect is encoded to base 64 to avoid problems with query strings
btoa(window.location.href)
)
.then((response) => {
// Navigante to the url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface WcaController {

@GetMapping("authentication")
AuthenticationResponse getWcaAuthenticationUrl(@Valid @NotBlank(message = "Frontend can not be blank") @RequestParam String frontendHost);
AuthenticationResponse getWcaAuthenticationUrl(@Valid @NotBlank(message = "Frontend can not be blank") @RequestParam String frontendHost, @Valid @NotBlank(message = "Redirect can not be blank") @RequestParam String redirect);

@GetMapping("user")
UserInfoDTO getUserInfo(@RequestHeader(value = "Authorization", required = false) String token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class WcaControllerImpl implements WcaController {
private WCAService wcaService;

@Override
public AuthenticationResponse getWcaAuthenticationUrl(String frontendHost) {
return wcaService.getWcaAuthenticationUrl(frontendHost);
public AuthenticationResponse getWcaAuthenticationUrl(String frontendHost, String redirect) {
return wcaService.getWcaAuthenticationUrl(frontendHost, redirect);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public class AuthenticationResponse {
private String url;
private String frontendHost;
private String redirect;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.worldcubeassociation.statistics.response.AuthenticationResponse;

public interface WCAService {
AuthenticationResponse getWcaAuthenticationUrl(String frontendHost);
AuthenticationResponse getWcaAuthenticationUrl(String frontendHost, String redirect);

UserInfoDTO getUserInfo(String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class WCAServiceImpl implements WCAService {
private WCAApi wcaApi;

@Override
public AuthenticationResponse getWcaAuthenticationUrl(String frontendHost) {
public AuthenticationResponse getWcaAuthenticationUrl(String frontendHost, String redirect) {
return AuthenticationResponse.builder().frontendHost(frontendHost).url(String
.format("%s/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=token&scope=public", wcaBaseUrl,
wcaAppId, frontendHost)).build();
.format("%s/oauth/authorize?client_id=%s&redirect_uri=%s?redirect=%s&response_type=token&scope=public", wcaBaseUrl,
wcaAppId, frontendHost, redirect)).build();
}

@Override
Expand Down

0 comments on commit 05432ee

Please sign in to comment.