Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0a1e9ee

Browse files
committed
set up authentication
1 parent04900eb commit0a1e9ee

File tree

7 files changed

+74
-6
lines changed

7 files changed

+74
-6
lines changed

‎typings/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export interface Action {
122122
exportinterfaceEnvironment{
123123
machineId:string
124124
sessionId:string
125+
token:string
125126
}
126127

127128
exportinterfaceMachineContext{
@@ -142,6 +143,7 @@ export interface MachineStateSchema {
142143
Start:{
143144
states:{
144145
Startup:{}
146+
Authenticate:{}
145147
NewOrContinue:{}
146148
SelectTutorial:{}
147149
ContinueTutorial:{}

‎web-app/src/Routes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Routes = () => {
1919
return(
2020
<Workspace>
2121
<Router>
22-
<Routepath={['Start.Startup','NewOrContinue']}>
22+
<Routepath={['Start.Startup','Start.Authenticate','Start.NewOrContinue']}>
2323
<LoadingPagetext="Launching..."/>
2424
</Route>
2525
<Routepath="Start.SelectTutorial">

‎web-app/src/services/apollo/auth.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import{Operation}from'apollo-boost'
2+
3+
letauthToken:string|null=null
4+
5+
exportconstsetAuthToken=(token:string|null)=>{
6+
authToken=token
7+
}
8+
9+
exportconstauthorizeHeaders=(operation:Operation)=>{
10+
operation.setContext({
11+
headers:{
12+
token:authToken
13+
}
14+
})
15+
}

‎web-app/src/services/apollo/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
importApolloClient,{InMemoryCache}from'apollo-boost'
22

3+
import{authorizeHeaders}from'./auth'
34
exportconstcache=newInMemoryCache()
45

56
constclient=newApolloClient({
67
uri:process.env.REACT_APP_GQL_URI,
7-
headers:{
8-
Authorization:process.env.REACT_APP_GQL_AUTH_TOKEN,
9-
},
8+
request:authorizeHeaders,
109
cache,
1110
})
1211

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import{gql}from'apollo-boost'
2+
3+
exportdefaultgql`
4+
mutation Authenticate(
5+
$machineId: String!,
6+
$sessionId: String!,
7+
$editor: EditorEnum!
8+
) {
9+
editorLogin(input: {
10+
machineId: $machineId,
11+
sessionId: $sessionId,
12+
editor: $editor
13+
}) {
14+
token
15+
user {
16+
id
17+
name
18+
email
19+
avatarUrl
20+
}
21+
}
22+
}
23+
`

‎web-app/src/services/state/actions/api.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import*asCRfrom'typings'
2+
importclientfrom'../../apollo'
3+
importauthenticateMutationfrom'../../apollo/mutations/authenticate'
4+
import{setAuthToken}from'../../apollo/auth'
5+
import{send}from'xstate'
26

37
exportdefault{
8+
authenticate:(async(context:CR.MachineContext):Promise<CR.Action>=>{
9+
constresult=awaitclient.mutate({
10+
mutation:authenticateMutation,
11+
variables:{
12+
machineId:context.env.machineId,
13+
sessionId:context.env.sessionId,
14+
editor:'VSCODE',
15+
}
16+
})
17+
18+
if(!result||!result.data){
19+
// TODO: handle failed authentication
20+
console.log('unauthenticated')
21+
}
22+
const{token}=result.data.editorLogin
23+
console.log(token)
24+
setAuthToken(token)
25+
returnsend({type:'AUTHENTICATED'})
26+
}),
427
userTutorialComplete(context:CR.MachineContext){
528
console.log('should update user tutorial as complete')
629
}

‎web-app/src/services/state/machine.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
1212
id:'root',
1313
initial:'Start',
1414
context:{
15-
env:{machineId:'',sessionId:''},
15+
env:{machineId:'',sessionId:'',token:''},
1616
tutorial:null,
1717
position:{levelId:'',stageId:'',stepId:''},
1818
progress:{
@@ -30,11 +30,17 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
3030
onEntry:['loadEnv'],
3131
on:{
3232
ENV_LOAD:{
33-
target:'NewOrContinue',
33+
target:'Authenticate',
3434
actions:['setEnv'],
3535
}
3636
}
3737
},
38+
Authenticate:{
39+
onEntry:['authenticate'],
40+
on:{
41+
AUTHENTICATED:'NewOrContinue'
42+
},
43+
},
3844
NewOrContinue:{
3945
onEntry:['loadStoredTutorial'],
4046
on:{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp