forked from gitpod-io/gitpod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
525051e
commit 188ad83
Showing
9 changed files
with
224 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset='utf-8'> | ||
<title>Login successful</title> | ||
<script> | ||
window.opener.postMessage("auth-success", `https://${window.location.hostname}`); | ||
</script> | ||
</head> | ||
<body> | ||
This browser tab is supposed to close automatically. | ||
</body> | ||
</html> |
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,30 +1,65 @@ | ||
import { AuthProviderInfo } from "@gitpod/gitpod-protocol"; | ||
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url"; | ||
import { useContext, useState } from "react"; | ||
import { useContext, useEffect, useState } from "react"; | ||
import Modal from "./components/Modal"; | ||
import { ServiceContext } from "./service/service"; | ||
import { UserContext } from "./contexts"; | ||
import { gitpodHostUrl, reconnect, service } from "./service/service"; | ||
|
||
export function Login() { | ||
const ctx = useContext(ServiceContext); | ||
const [authProvider, setAuthProvider]= useState([] as AuthProviderInfo[]); | ||
ctx.service.server.getAuthProviders().then( | ||
aps => setAuthProvider(aps) | ||
); | ||
const { setUser } = useContext(UserContext); | ||
|
||
const [authProviders, setAuthProviders] = useState<AuthProviderInfo[]>([]); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
setAuthProviders(await service.getAuthProviders()); | ||
})(); | ||
|
||
window.addEventListener("message", (event) => { | ||
// todo: check event.origin | ||
|
||
if (event.data === "auth-success") { | ||
if (event.source && "close" in event.source && event.source.close) { | ||
console.log(`try to close window`); | ||
event.source.close(); | ||
} else { | ||
// todo: not here, but add a button to the /login-success page to close, if this should not work as expected | ||
} | ||
(async () => { | ||
reconnect(); | ||
const user = await service.reloadUser(); | ||
setUser(user); | ||
})(); | ||
} | ||
}) | ||
}) | ||
|
||
const openLogin = (host: string) => { | ||
const url = getLoginUrl(host); | ||
const newWindow = window.open(url, "gitpod-login"); | ||
if (!newWindow) { | ||
console.log(`Failed to open login window for ${host}`); | ||
} | ||
} | ||
|
||
return (<div> | ||
<Modal visible={true}> | ||
{authProvider.map(ap => { | ||
return (<a href={getLoginUrl(ap.host)} target="_parent">Login With GitHub</a>); | ||
})} | ||
<div> | ||
<ol> | ||
{authProviders.map(ap => { | ||
return (<li> | ||
<h2><a href="#" onClick={() => openLogin(ap.host)}>Continue with {ap.host}</a></h2> | ||
</li>); | ||
})} | ||
</ol> | ||
</div> | ||
</Modal> | ||
</div>); | ||
} | ||
|
||
function getLoginUrl(host: string) { | ||
const returnTo = "https://google.com"; | ||
const returnToPart = returnTo ? `&returnTo=${encodeURIComponent(returnTo)}` : ''; | ||
const search = `host=${host}${returnToPart}`; | ||
return new GitpodHostUrl(window.location.toString()).withApi({ | ||
pathname: '/login/', | ||
search | ||
const returnTo = gitpodHostUrl.with({ pathname: 'login-success'}).toString(); | ||
return gitpodHostUrl.withApi({ | ||
pathname: '/login', | ||
search: `host=${host}&returnTo=${encodeURIComponent(returnTo)}` | ||
}).toString(); | ||
} |
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,22 @@ | ||
import { User } from '@gitpod/gitpod-protocol'; | ||
import React, { createContext, useState } from 'react'; | ||
|
||
const UserContext = createContext<{ | ||
user?: User, | ||
setUser: React.Dispatch<User> | ||
}>({ | ||
setUser: () => null | ||
}); | ||
|
||
|
||
const AppProvider: React.FC = ({ children }) => { | ||
const [user, setUser] = useState<User | undefined>(undefined); | ||
|
||
return ( | ||
<UserContext.Provider value={{ user, setUser }}> | ||
{children} | ||
</UserContext.Provider> | ||
) | ||
} | ||
|
||
export { UserContext, AppProvider }; |
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,18 +1,18 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import "./tailwind.output.css" | ||
import App from './App'; | ||
import { ServiceContext, SimpleServiceImpl } from './service/service'; | ||
import { AppProvider } from './contexts'; | ||
import { service } from './service/service'; | ||
|
||
import "./tailwind.output.css" | ||
|
||
service.getOrLoadUser().then(user => console.log(user.name)); | ||
|
||
const service = new SimpleServiceImpl(); | ||
service.service.server.getLoggedInUser().then(user => { | ||
service.user = user; | ||
ReactDOM.render( | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<ServiceContext.Provider value={service}> | ||
<App /> | ||
</ServiceContext.Provider> | ||
<AppProvider> | ||
<App /> | ||
</AppProvider> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
); | ||
}); | ||
); |
Oops, something went wrong.