main
rb 40 lines 1.37 KB
Raw
1 # frozen_string_literal: true
2
3 module Api
4 module V1
5 # Token-based sign up for API clients.
6 class RegistrationsController < Api::BaseController
7 # POST /api/v1/auth/sign_up — returns SignupResult.
8 # Params: email, password
9 def create
10 email = params[:email].to_s.strip.downcase
11 password = params[:password].to_s
12
13 if email.blank? || password.blank?
14 return render_error(ERR_INVALID, "Email and password are required.", status: :unprocessable_entity)
15 end
16
17 if password.length < 8
18 return render_error(ERR_INVALID, "Password must be at least 8 characters.", status: :unprocessable_entity)
19 end
20
21 result = SmbcloudAuthService.signup(email: email, password: password)
22 render json: signup_result_json(result), status: :ok
23 end
24
25 private
26
27 # SignupResult — { code: Option<i32>, message: String, data: Option<Data> }.
28 # The gem already returns this shape; normalise keys and guarantee a message.
29 def signup_result_json(result)
30 result = result.respond_to?(:symbolize_keys) ? result.symbolize_keys : result.to_h
31 {
32 code: result[:code],
33 message: result[:message].presence ||
34 "Signed up successfully. Please check your email to confirm your account.",
35 data: result[:data]
36 }
37 end
38 end
39 end
40 end