onboarding.mjs 6.6 KB

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