Skip to content

Feature/auth #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export interface Action {
export interface Environment {
machineId: string
sessionId: string
token: string
}

export interface MachineContext {
Expand All @@ -142,6 +143,7 @@ export interface MachineStateSchema {
Start: {
states: {
Startup: {}
Authenticate: {}
NewOrContinue: {}
SelectTutorial: {}
ContinueTutorial: {}
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Routes = () => {
return (
<Workspace>
<Router>
<Route path={['Start.Startup', 'NewOrContinue']}>
<Route path={['Start.Startup', 'Start.Authenticate', 'Start.NewOrContinue']}>
<LoadingPage text="Launching..." />
</Route>
<Route path="Start.SelectTutorial">
Expand Down
4 changes: 2 additions & 2 deletions web-app/src/components/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ErrorBoundary extends React.Component {
// Display fallback UI
this.setState({ hasError: true })
// You can also log the error to an error reporting service
console.error(error)
console.log(info)
console.error(JSON.stringify(error))
console.log(JSON.stringify(info))
}

public render() {
Expand Down
17 changes: 17 additions & 0 deletions web-app/src/services/apollo/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Operation} from 'apollo-boost'

let authToken: string | null = null

export const setAuthToken = (token: string | null) => {
authToken = token
}

export const authorizeHeaders = (operation: Operation) => {
if (authToken) {
operation.setContext({
headers: {
'Authorization': authToken
}
})
}
}
5 changes: 2 additions & 3 deletions web-app/src/services/apollo/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import ApolloClient, {InMemoryCache} from 'apollo-boost'

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

const client = new ApolloClient({
uri: process.env.REACT_APP_GQL_URI,
headers: {
Authorization: process.env.REACT_APP_GQL_AUTH_TOKEN,
},
request: authorizeHeaders,
cache,
})

Expand Down
23 changes: 23 additions & 0 deletions web-app/src/services/apollo/mutations/authenticate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {gql} from 'apollo-boost'

export default gql`
mutation Authenticate(
$machineId: String!,
$sessionId: String!,
$editor: EditorEnum!
) {
editorLogin(input: {
machineId: $machineId,
sessionId: $sessionId,
editor: $editor
}) {
token
user {
id
name
email
avatarUrl
}
}
}
`
24 changes: 1 addition & 23 deletions web-app/src/services/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,17 @@ class Channel {
// messages from core
switch (action.type) {
case 'ENV_LOAD':
this.machineSend(action)
return
case 'AUTHENTICATED':
case 'TUTORIAL_LOADED':
// send action to state machine
this.machineSend('TUTORIAL_LOADED')
console.log('send action to state machine')
return
case 'NEW_TUTORIAL':
this.machineSend(action)
return
case 'TUTORIAL_CONFIGURED':
this.machineSend(action)
return
case 'CONTINUE_TUTORIAL':
this.machineSend(action)
return
case 'TEST_PASS':
// { type: 'TEST_PASS', payload: { stepId: string }}
this.machineSend(action)
return
case 'TEST_FAIL':
this.machineSend(action)
return
case 'TEST_RUNNING':
this.machineSend(action)
return
case 'TEST_ERROR':
console.log('TEST_ERROR')
this.machineSend(action)
return
case 'ACTIONS_LOADED':
// TODO: use this for verifying completion of stepActions
return
default:
if (action.type) {
console.warn(`Unknown received action ${action.type}`, action)
Expand Down
26 changes: 26 additions & 0 deletions web-app/src/services/state/actions/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import * as CR from 'typings'
import client from '../../apollo'
import authenticateMutation from '../../apollo/mutations/authenticate'
import {setAuthToken} from '../../apollo/auth'
import channel from '../../../services/channel'

export default {
authenticate: (async (context: CR.MachineContext): Promise<void> => {

const result = await client.mutate({
mutation: authenticateMutation,
variables: {
machineId: context.env.machineId,
sessionId: context.env.sessionId,
editor: 'VSCODE',
}
})


if (!result || !result.data) {
// TODO: handle failed authentication
console.error('ERROR: Authentication failed')
}
const {token} = result.data.editorLogin
// add token to headers
setAuthToken(token)
// pass authenticated action back to state machine
channel.receive({data: {type: 'AUTHENTICATED'}})
}),
userTutorialComplete(context: CR.MachineContext) {
console.log('should update user tutorial as complete')
}
Expand Down
5 changes: 4 additions & 1 deletion web-app/src/services/state/actions/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import * as selectors from '../../selectors'
export default {
setEnv: assign({
env: (context: CR.MachineContext, event: CR.MachineEvent) => {
return event.payload.env
return {
...context.env,
...event.payload.env
}
}
}),
continueTutorial: assign({
Expand Down
10 changes: 8 additions & 2 deletions web-app/src/services/state/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
id: 'root',
initial: 'Start',
context: {
env: {machineId: '', sessionId: ''},
env: {machineId: '', sessionId: '', token: ''},
tutorial: null,
position: {levelId: '', stageId: '', stepId: ''},
progress: {
Expand All @@ -30,11 +30,17 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
onEntry: ['loadEnv'],
on: {
ENV_LOAD: {
target: 'NewOrContinue',
target: 'Authenticate',
actions: ['setEnv'],
}
}
},
Authenticate: {
onEntry: ['authenticate'],
on: {
AUTHENTICATED: 'NewOrContinue'
},
},
NewOrContinue: {
onEntry: ['loadStoredTutorial'],
on: {
Expand Down