onboarding.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 } = {}) {
  12. if (this.state && this.busy) throw new Error('当前步骤尚未完成,请稍候')
  13. this.state = { active: true, mode, serverURL, remember, forceLogin, phase: 'idle', stage: 'checking',
  14. setupRequired: false, 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'
  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, 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,
  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. }
  61. return this.snapshot()
  62. }
  63. async submit({ username, password, confirmation, remember = this.state.remember } = {}) {
  64. if (this.state.phase !== 'ready') throw new Error('请先完成服务器检测')
  65. this.state.remember = remember === true
  66. this.state.error = ''
  67. if (this.state.authenticated) return this.enter()
  68. if (!String(username || '').trim() || !password) throw new Error('请填写用户名和密码')
  69. if (this.state.setupRequired && (password.length < 8 || password !== confirmation))
  70. throw new Error(password.length < 8 ? '密码至少需要 8 个字符' : '两次输入的密码不一致')
  71. this.state.phase = 'authenticating'
  72. try {
  73. const result = await this.options.authenticate({ mode: this.state.mode, serverURL: this.state.serverURL,
  74. setup: this.state.mode === 'local' && this.state.setupRequired, username, password })
  75. this.state.authenticated = true
  76. this.state.username = result.username
  77. this.state.phase = 'ready'
  78. if (result.warning) {
  79. this.state.error = result.warning
  80. this.state.message = '账号已登录,可以继续进入夭夭'
  81. } else await this.enter()
  82. } catch (error) {
  83. this.state.phase = 'ready'
  84. this.state.error = error.message || '登录未完成,请重试'
  85. }
  86. return this.snapshot()
  87. }
  88. async enter() {
  89. this.state.phase = 'entering'
  90. try {
  91. await this.options.activate(this.snapshot())
  92. this.state.active = false
  93. this.state.phase = 'complete'
  94. await this.options.navigate(this.state.serverURL)
  95. } catch (error) {
  96. this.state.active = true
  97. this.state.phase = 'ready'
  98. this.state.error = error.message || '打开服务未完成,请重试'
  99. }
  100. return this.snapshot()
  101. }
  102. }