Skip to content

Commit

Permalink
feat(ui): register with name (#2248)
Browse files Browse the repository at this point in the history
* feat(ui): register with name

* fix test

* name required in registration

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wwayne and autofix-ci[bot] authored May 27, 2024
1 parent 571b75c commit 8c956b9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ee/tabby-schema/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ type Mutation {
updateUserRole(id: ID!, isAdmin: Boolean!): Boolean!
uploadUserAvatarBase64(id: ID!, avatarBase64: String): Boolean!
updateUserName(id: ID!, name: String!): Boolean!
register(email: String!, password1: String!, password2: String!, invitationCode: String): RegisterResponse!
register(email: String!, password1: String!, password2: String!, invitationCode: String, name: String!): RegisterResponse!
tokenAuth(email: String!, password: String!): TokenAuthResponse!
verifyToken(token: String!): Boolean!
refreshToken(refreshToken: String!): RefreshTokenResponse!
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-schema/src/schema/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ pub trait AuthenticationService: Send + Sync {
email: String,
password1: String,
invitation_code: Option<String>,
name: Option<String>,
) -> Result<RegisterResponse>;
async fn allow_self_signup(&self) -> Result<bool>;

Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-schema/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ impl Mutation {
password1: String,
password2: String,
invitation_code: Option<String>,
name: String,
) -> Result<RegisterResponse> {
let input = auth::RegisterInput {
email,
Expand All @@ -844,7 +845,7 @@ impl Mutation {

ctx.locator
.auth()
.register(input.email, input.password1, invitation_code)
.register(input.email, input.password1, invitation_code, Some(name))
.await
}

Expand Down
16 changes: 16 additions & 0 deletions ee/tabby-ui/app/auth/signup/components/user-register-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import {

export const registerUser = graphql(/* GraphQL */ `
mutation register(
$name: String!
$email: String!
$password1: String!
$password2: String!
$invitationCode: String
) {
register(
name: $name
email: $email
password1: $password1
password2: $password2
Expand All @@ -48,6 +50,7 @@ export const registerUser = graphql(/* GraphQL */ `
`)

const formSchema = z.object({
name: z.string(),
email: z.string().email('Invalid email address'),
password1: z.string(),
password2: z.string(),
Expand Down Expand Up @@ -104,6 +107,19 @@ export function UserAuthForm({
<Form {...form}>
<div className={cn('grid gap-2', className)} {...props}>
<form className="grid gap-4" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input {...field} value={field.value} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="email"
Expand Down
30 changes: 22 additions & 8 deletions ee/tabby-webserver/src/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl AuthenticationService for AuthenticationServiceImpl {
email: String,
password: String,
invitation_code: Option<String>,
name: Option<String>,
) -> Result<RegisterResponse> {
let is_admin_initialized = self.is_admin_initialized().await?;
if is_admin_initialized && is_demo_mode() {
Expand All @@ -85,12 +86,17 @@ impl AuthenticationService for AuthenticationServiceImpl {
Some(pwd_hash),
!is_admin_initialized,
invitation.id,
None,
name.clone(),
)
.await?
} else {
self.db
.create_user(email.clone(), Some(pwd_hash), !is_admin_initialized, None)
.create_user(
email.clone(),
Some(pwd_hash),
!is_admin_initialized,
name.clone(),
)
.await?
};

Expand Down Expand Up @@ -736,7 +742,12 @@ mod tests {

async fn register_admin_user(service: &AuthenticationServiceImpl) -> RegisterResponse {
service
.register(ADMIN_EMAIL.to_owned(), ADMIN_PASSWORD.to_owned(), None)
.register(
ADMIN_EMAIL.to_owned(),
ADMIN_PASSWORD.to_owned(),
None,
None,
)
.await
.unwrap()
}
Expand Down Expand Up @@ -791,7 +802,7 @@ mod tests {
// Admin initialized, registeration requires a invitation code;
assert_matches!(
service
.register(email.to_owned(), password.to_owned(), None)
.register(email.to_owned(), password.to_owned(), None, None)
.await,
Err(_)
);
Expand All @@ -802,7 +813,8 @@ mod tests {
.register(
email.to_owned(),
password.to_owned(),
Some("abc".to_owned())
Some("abc".to_owned()),
None
)
.await,
Err(_)
Expand All @@ -814,6 +826,7 @@ mod tests {
email.to_owned(),
password.to_owned(),
Some(invitation.code.clone()),
None
)
.await
.is_ok());
Expand All @@ -824,7 +837,8 @@ mod tests {
.register(
email.to_owned(),
password.to_owned(),
Some(invitation.code.clone())
Some(invitation.code.clone()),
None
)
.await,
Err(_)
Expand Down Expand Up @@ -1017,7 +1031,7 @@ mod tests {
.unwrap();

service
.register("[email protected]".into(), "".into(), Some(code.code))
.register("[email protected]".into(), "".into(), Some(code.code), None)
.await
.unwrap();

Expand Down Expand Up @@ -1303,7 +1317,7 @@ mod tests {

// Create owner user.
service
.register("[email protected]".into(), "pass".into(), None)
.register("[email protected]".into(), "pass".into(), None, None)
.await
.unwrap();

Expand Down

0 comments on commit 8c956b9

Please sign in to comment.