-
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.
Merge pull request #3 from bannzai/fix/state/management
Fix state meanagement(WIP)
- Loading branch information
Showing
14 changed files
with
24,675 additions
and
876 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ web-build/ | |
# macOS | ||
.DS_Store | ||
app.config.js | ||
|
||
schema.json |
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,41 +1,29 @@ | ||
import * as React from "react"; | ||
import * as WebBrowser from "expo-web-browser"; | ||
import { makeRedirectUri, useAuthRequest } from "expo-auth-session"; | ||
import { Button } from "react-native"; | ||
import Constants from "expo-constants"; | ||
import { Platform } from "react-native"; | ||
import { BrowserRouter, Redirect, Switch } from "react-router-dom"; | ||
import { AuthorizedRoute } from "./src/route/AuthorizedRoute"; | ||
import { HomePage } from "./src/usecase/home/HomePage"; | ||
import { UnAuthorizedRoute } from "./src/route/UnauthorizedRoute"; | ||
import { LoginPage } from "./src/usecase/login/LoginPage"; | ||
import { AuthProvider } from "./src/context/Auth"; | ||
|
||
WebBrowser.maybeCompleteAuthSession(); | ||
|
||
// Endpoint | ||
const discovery = { | ||
authorizationEndpoint: "https://github.com/login/oauth/authorize", | ||
tokenEndpoint: "https://github.com/login/oauth/access_token", | ||
revocationEndpoint: `https://github.com/settings/connections/applications/${Constants.manifest?.extra?.githubOAuthClientID}`, | ||
}; | ||
|
||
export default function App() { | ||
const [request, response, promptAsync] = useAuthRequest( | ||
{ | ||
clientId: Constants.manifest?.extra?.githubOAuthClientID, | ||
redirectUri: Constants.manifest?.extra?.githubOAuthCallbackURL, | ||
}, | ||
discovery | ||
); | ||
|
||
React.useEffect(() => { | ||
if (response?.type === "success") { | ||
const { code } = response.params; | ||
console.log(`success code: ${code}`); | ||
} | ||
}, [response]); | ||
|
||
return ( | ||
<Button | ||
disabled={!request} | ||
title="Login" | ||
onPress={() => { | ||
promptAsync(); | ||
}} | ||
/> | ||
); | ||
if (Platform.OS === "web") { | ||
return ( | ||
<AuthProvider> | ||
<BrowserRouter> | ||
<Switch> | ||
<AuthorizedRoute exact path="/" component={HomePage} /> | ||
<UnAuthorizedRoute exact path="/login" component={LoginPage} /> | ||
<Redirect to="/" /> | ||
</Switch> | ||
</BrowserRouter> | ||
</AuthProvider> | ||
); | ||
} else { | ||
return <p> Comming soon...</p>; | ||
} | ||
} |
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,11 @@ | ||
schema: ./schema.json | ||
documents: "src/**/*.graphql" | ||
generates: | ||
src/generated-types.ts: | ||
plugins: | ||
- typescript | ||
- typescript-operations | ||
- typescript-react-apollo | ||
config: | ||
withHooks: true | ||
|
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,17 @@ | ||
query Top($userName: String!, $after: String) { | ||
user(login: $userName) { | ||
id | ||
name | ||
avatarUrl | ||
repositories(first: 20, after: $after) { | ||
edges { | ||
node { | ||
id | ||
name | ||
url | ||
viewerHasStarred | ||
} | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
import React, { createContext, useContext, useState } from "react"; | ||
import { IdentifyToken } from "../model/IdentifyToken"; | ||
|
||
type Login = (token: IdentifyToken) => void; | ||
const LoginContext = createContext<Login>((_) => | ||
console.error("Unexpected not set login handler") | ||
); | ||
const IdentifyTokenContext = createContext<IdentifyToken | null>(null); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const AuthProvider: React.VFC<Props> = ({ children }) => { | ||
const [identifyToken, setIdentifyToken] = useState<IdentifyToken | null>( | ||
null | ||
); | ||
|
||
console.log(`identifyToken: ${identifyToken}`); | ||
const login = (token: IdentifyToken) => { | ||
console.log(`called setIdentifyToken`); | ||
setIdentifyToken(token); | ||
}; | ||
|
||
return ( | ||
<LoginContext.Provider value={login}> | ||
<IdentifyTokenContext.Provider value={identifyToken}> | ||
{children} | ||
</IdentifyTokenContext.Provider> | ||
</LoginContext.Provider> | ||
); | ||
}; | ||
|
||
export const useIdentifyToken = () => useContext(IdentifyTokenContext); | ||
export const useLogin = () => useContext(LoginContext); |
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,30 @@ | ||
import { | ||
ApolloClient, | ||
createHttpLink, | ||
NormalizedCacheObject, | ||
} from "@apollo/client/core"; | ||
import { setContext } from "@apollo/client/link/context"; | ||
|
||
import { InMemoryCache } from "@apollo/client/cache"; | ||
|
||
const httpLink = createHttpLink({ | ||
uri: "https://api.github.com/graphql", | ||
}); | ||
const authLink = setContext((_, { headers }) => { | ||
const token = localStorage.getItem("github_token"); | ||
return { | ||
headers: { | ||
...headers, | ||
authorization: token ? `Bearer ${token}` : "", | ||
}, | ||
}; | ||
}); | ||
|
||
export function createApolloClient( | ||
token: string | ||
): ApolloClient<NormalizedCacheObject> { | ||
return new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
link: httpLink.concat(authLink), | ||
}); | ||
} |
Oops, something went wrong.