about summary refs log tree commit diff
path: root/src/state/models/session.ts
blob: c032d759421480e0c21151cb0fbbd82bd8085d3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {Instance, SnapshotOut, types, flow} from 'mobx-state-tree'
// import {UserConfig} from '../../api'
import * as auth from '../auth'
import {withEnvironment} from '../env'

export const SessionModel = types
  .model('Session')
  .props({
    isAuthed: types.boolean,
    uiIsProcessing: types.maybe(types.boolean),
    uiError: types.maybe(types.string),

    // TODO: these should be stored somewhere secret
    serverUrl: types.maybe(types.string),
    secretKeyStr: types.maybe(types.string),
    rootAuthToken: types.maybe(types.string),
  })
  .extend(withEnvironment)
  .actions(self => ({
    setAuthed: (v: boolean) => {
      self.isAuthed = v
    },
    login: flow(function* () {
      self.uiIsProcessing = true
      self.uiError = undefined
      try {
        if (!self.environment.authStore) {
          throw new Error('Auth store not initialized')
        }
        const res = yield auth.requestAppUcan(self.environment.authStore)
        self.isAuthed = res
        self.uiIsProcessing = false
        return res
      } catch (e: any) {
        console.error('Failed to request app ucan', e)
        self.uiError = e.toString()
        self.uiIsProcessing = false
        return false
      }
    }),
    logout: flow(function* () {
      self.uiIsProcessing = true
      self.uiError = undefined
      try {
        if (!self.environment.authStore) {
          throw new Error('Auth store not initialized')
        }
        const res = yield auth.logout(self.environment.authStore)
        self.isAuthed = false
        self.uiIsProcessing = false
        return res
      } catch (e: any) {
        console.error('Failed to log out', e)
        self.uiError = e.toString()
        self.uiIsProcessing = false
        return false
      }
    }),
    /*loadAccount: flow(function* () {
      self.uiIsProcessing = true
      self.uiError = undefined
      try {
        // const cfg = yield UserConfig.hydrate({
        //   serverUrl: self.serverUrl,
        //   secretKeyStr: self.secretKeyStr,
        //   rootAuthToken: self.rootAuthToken,
        // })
        // self.environment.api.setUserCfg(cfg)
        self.isAuthed = true
        self.uiIsProcessing = false
        return true
      } catch (e: any) {
        console.error('Failed to create test account', e)
        self.uiError = e.toString()
        self.uiIsProcessing = false
        return false
      }
    }),
    createTestAccount: flow(function* (_serverUrl: string) {
      self.uiIsProcessing = true
      self.uiError = undefined
      try {
        // const cfg = yield UserConfig.createTest(serverUrl)
        // const state = yield cfg.serialize()
        // self.serverUrl = state.serverUrl
        // self.secretKeyStr = state.secretKeyStr
        // self.rootAuthToken = state.rootAuthToken
        self.isAuthed = true
        // self.environment.api.setUserCfg(cfg)
      } catch (e: any) {
        console.error('Failed to create test account', e)
        self.uiError = e.toString()
      }
      self.uiIsProcessing = false
    }),*/
  }))

export interface Session extends Instance<typeof SessionModel> {}
export interface SessionSnapshot extends SnapshotOut<typeof SessionModel> {}

export function createDefaultSession() {
  return {
    isAuthed: false,
    uiState: 'idle',
  }
}