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

Commit31f58aa

Browse files
authored
Merge pull requestcoderoad#41 from ShMcK/feature/auth
Feature/auth
2 parents04900eb +8b32f18 commit31f58aa

File tree

10 files changed

+86
-32
lines changed

10 files changed

+86
-32
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
export interface Environment {
123123
machineId: string
124124
sessionId: string
125+
token: string
125126
}
126127

127128
export interface MachineContext {
@@ -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-
<Route path={['Start.Startup', 'NewOrContinue']}>
22+
<Route path={['Start.Startup', 'Start.Authenticate', 'Start.NewOrContinue']}>
2323
<LoadingPage text="Launching..." />
2424
</Route>
2525
<Route path="Start.SelectTutorial">

‎web-app/src/components/ErrorBoundary/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class ErrorBoundary extends React.Component {
77
// Display fallback UI
88
this.setState({ hasError: true })
99
// You can also log the error to an error reporting service
10-
console.error(error)
11-
console.log(info)
10+
console.error(JSON.stringify(error))
11+
console.log(JSON.stringify(info))
1212
}
1313

1414
public render() {

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

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

‎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
import ApolloClient, {InMemoryCache} from 'apollo-boost'
22

3+
import {authorizeHeaders} from './auth'
34
export const cache = new InMemoryCache()
45

56
const client = new ApolloClient({
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+
export default gql`
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/channel/index.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,17 @@ class Channel {
3838
// messages from core
3939
switch (action.type) {
4040
case 'ENV_LOAD':
41-
this.machineSend(action)
42-
return
41+
case 'AUTHENTICATED':
4342
case 'TUTORIAL_LOADED':
44-
// send action to state machine
45-
this.machineSend('TUTORIAL_LOADED')
46-
console.log('send action to state machine')
47-
return
4843
case 'NEW_TUTORIAL':
49-
this.machineSend(action)
50-
return
5144
case 'TUTORIAL_CONFIGURED':
52-
this.machineSend(action)
53-
return
5445
case 'CONTINUE_TUTORIAL':
55-
this.machineSend(action)
56-
return
5746
case 'TEST_PASS':
58-
// { type: 'TEST_PASS', payload: { stepId: string }}
59-
this.machineSend(action)
60-
return
6147
case 'TEST_FAIL':
62-
this.machineSend(action)
63-
return
6448
case 'TEST_RUNNING':
65-
this.machineSend(action)
66-
return
6749
case 'TEST_ERROR':
68-
console.log('TEST_ERROR')
6950
this.machineSend(action)
7051
return
71-
case 'ACTIONS_LOADED':
72-
// TODO: use this for verifying completion of stepActions
73-
return
7452
default:
7553
if (action.type) {
7654
console.warn(`Unknown received action ${action.type}`, action)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
import * as CR from 'typings'
2+
import client from '../../apollo'
3+
import authenticateMutation from '../../apollo/mutations/authenticate'
4+
import {setAuthToken} from '../../apollo/auth'
5+
import channel from '../../../services/channel'
26

37
export default {
8+
authenticate: (async (context: CR.MachineContext): Promise<void> => {
9+
10+
const result = await client.mutate({
11+
mutation: authenticateMutation,
12+
variables: {
13+
machineId: context.env.machineId,
14+
sessionId: context.env.sessionId,
15+
editor: 'VSCODE',
16+
}
17+
})
18+
19+
20+
if (!result || !result.data) {
21+
// TODO: handle failed authentication
22+
console.error('ERROR: Authentication failed')
23+
}
24+
const {token} = result.data.editorLogin
25+
// add token to headers
26+
setAuthToken(token)
27+
// pass authenticated action back to state machine
28+
channel.receive({data: {type: 'AUTHENTICATED'}})
29+
}),
430
userTutorialComplete(context: CR.MachineContext) {
531
console.log('should update user tutorial as complete')
632
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import * as selectors from '../../selectors'
66
export default {
77
setEnv: assign({
88
env: (context: CR.MachineContext, event: CR.MachineEvent) => {
9-
return event.payload.env
9+
return {
10+
...context.env,
11+
...event.payload.env
12+
}
1013
}
1114
}),
1215
continueTutorial: assign({

‎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