onboarding.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { normalizeServerURL } from './remote-login.mjs'
  2. /** The native onboarding page owns one selected server and one in-flight action.
  3. * Credentials and CSRF/session tokens never appear in renderer snapshots. */
  4. export class DesktopOnboarding {
  5. constructor(options) {
  6. this.options = options
  7. this.open()
  8. }
  9. get busy() { return ['preparing', 'authenticating', 'entering'].includes(this.state.phase) }
  10. snapshot() { return { ...this.state, history: [...this.state.history], busy: this.busy,
  11. platform: this.options.platform ?? process.platform, supportedModes: this.options.supportedModes ?? ['client', 'server'] } }
  12. open({ mode = null, serverURL = '', remember = false, forceLogin = false, restoring = false } = {}) {
  13. if (this.state && this.busy) throw new Error('当前步骤尚未完成,请稍候')
  14. if (mode === 'local' && !this.snapshotModes().includes('server')) throw new Error('此客户端不支持在本机运行服务器')
  15. if (!mode && !this.snapshotModes().includes('server')) mode = 'remote'
  16. this.state = { active: true, mode, serverURL, remember, forceLogin, restoring, phase: 'idle', stage: 'checking',
  17. setupRequired: false, registrationAvailable: false, registrationNotice: '', authenticated: false, username: '', message: '', error: '',
  18. canForceSync: false, history: [] }
  19. return this.snapshot()
  20. }
  21. select(mode) {
  22. if (mode === null) return this.open({ remember: this.state.remember })
  23. if (!['local', 'remote'].includes(mode)) throw new Error('请选择运行方式')
  24. return this.open({ mode, serverURL: this.options.remoteServer(), remember: this.state.remember })
  25. }
  26. snapshotModes() { return this.options.supportedModes ?? ['client', 'server'] }
  27. serviceChanged(service) {
  28. if (!this.state.active || this.state.mode !== 'local') return
  29. this.state.stage = service.stage || this.state.stage
  30. this.state.message = service.message
  31. this.state.canForceSync = service.canForceSync === true
  32. if (this.state.history.at(-1) !== service.message) {
  33. this.state.history.push(service.message)
  34. this.state.history = this.state.history.slice(-30)
  35. }
  36. if (service.phase === 'error') {
  37. this.state.error = service.message
  38. // Keep the action locked until the outstanding prepare operation settles.
  39. if (!this.busy) { this.state.phase = 'error'; this.state.restoring = false }
  40. }
  41. if (['error', 'restarting', 'stopped', 'disconnected'].includes(service.phase)) {
  42. this.state.authenticated = false
  43. if (this.state.phase === 'ready') this.state.phase = 'error'
  44. }
  45. }
  46. async prepare({ serverURL = this.state.serverURL, remember = this.state.remember, force = false, autoEnter = false } = {}) {
  47. if (this.busy) throw new Error('当前步骤尚未完成,请稍候')
  48. const mode = this.state.mode
  49. if (!mode) throw new Error('请先选择服务器')
  50. Object.assign(this.state, { phase: 'preparing', error: '', authenticated: false,
  51. setupRequired: false, registrationAvailable: false, registrationNotice: '', remember: remember === true, stage: 'checking', canForceSync: false,
  52. message: mode === 'local' ? '正在检查本机环境…' : '正在检测服务器连接…' })
  53. try {
  54. const target = mode === 'local' ? await this.options.prepareLocal({ force }) : normalizeServerURL(serverURL)
  55. this.state.serverURL = target
  56. const info = mode === 'local' && this.options.localSession
  57. ? await this.options.localSession(target) : await this.options.inspect(target)
  58. if (mode === 'remote' && info.setupRequired) throw new Error('远程服务器尚未初始化,请先在服务器本机创建管理员,再重新检测。')
  59. Object.assign(this.state, { phase: 'ready', stage: 'ready', setupRequired: info.setupRequired === true, registrationAvailable: info.registrationAvailable === true,
  60. authenticated: info.authenticated === true && !this.state.forceLogin, username: info.user?.username || '',
  61. message: mode === 'local' ? '本机服务已就绪' : '连接成功,服务器已就绪' })
  62. if ((autoEnter || mode === 'local' && this.options.localSession) && this.state.authenticated) await this.enter()
  63. } catch (error) {
  64. this.state.phase = 'error'
  65. this.state.error = error.message || '准备未完成,请重试'
  66. } finally {
  67. // Keep the guide hidden through successful navigation. Only reveal it
  68. // when restoring needs user input, including connection/navigation errors.
  69. if (this.state.active) this.state.restoring = false
  70. }
  71. return this.snapshot()
  72. }
  73. async submit({ username, password, confirmation, register = false, remember = this.state.remember } = {}) {
  74. if (this.state.phase !== 'ready') throw new Error('请先完成服务器检测')
  75. this.state.remember = remember === true
  76. this.state.error = ''
  77. if (register) return this.register({ username, password, confirmation })
  78. if (this.state.authenticated) return this.enter()
  79. if (!String(username || '').trim() || !password) throw new Error('请填写用户名和密码')
  80. if (this.state.setupRequired && (password.length < 8 || password !== confirmation))
  81. throw new Error(password.length < 8 ? '密码至少需要 8 个字符' : '两次输入的密码不一致')
  82. this.state.phase = 'authenticating'
  83. try {
  84. const result = await this.options.authenticate({ mode: this.state.mode, serverURL: this.state.serverURL,
  85. setup: this.state.mode === 'local' && this.state.setupRequired, username, password })
  86. this.state.authenticated = true
  87. this.state.username = result.username
  88. this.state.phase = 'ready'
  89. if (result.warning) {
  90. this.state.error = result.warning
  91. this.state.message = '账号已登录,可以继续进入夭夭'
  92. } else await this.enter()
  93. } catch (error) {
  94. this.state.phase = 'ready'
  95. this.state.error = error.message || '登录未完成,请重试'
  96. }
  97. return this.snapshot()
  98. }
  99. async register({ username, password, confirmation }) {
  100. if (!this.state.registrationAvailable || this.state.setupRequired || this.state.authenticated) throw new Error('当前服务器尚不支持子账号注册,请联系管理员初始化或升级。')
  101. if (!String(username || '').trim() || !password || password.length < 8 || password !== confirmation) throw new Error('请填写用户名及至少 8 位密码,并确认两次密码一致。')
  102. this.state.phase = 'authenticating'
  103. this.state.registrationNotice = ''
  104. try {
  105. await this.options.register({ serverURL: this.state.serverURL, username, password })
  106. this.state.registrationNotice = '注册成功,请等待管理员开通。开通后使用此账号登录。'
  107. } catch (error) { this.state.error = error.message || '注册失败,请重试。' }
  108. finally { this.state.phase = 'ready' }
  109. return this.snapshot()
  110. }
  111. async enter() {
  112. this.state.phase = 'entering'
  113. try {
  114. await this.options.activate(this.snapshot())
  115. this.state.active = false
  116. this.state.phase = 'complete'
  117. await this.options.navigate(this.state.serverURL)
  118. } catch (error) {
  119. this.state.active = true
  120. this.state.phase = 'ready'
  121. this.state.error = error.message || '打开服务未完成,请重试'
  122. }
  123. return this.snapshot()
  124. }
  125. }