Browse Source

add init database script, fix login/register styling

Alexander Belanger 5 years ago
parent
commit
fd07fc74c0
3 changed files with 83 additions and 58 deletions
  1. 3 3
      dashboard/src/main/auth/Login.tsx
  2. 59 50
      dashboard/src/main/auth/Register.tsx
  3. 21 5
      internal/adapter/gorm.go

+ 3 - 3
dashboard/src/main/auth/Login.tsx

@@ -215,7 +215,7 @@ export default class Login extends Component<PropsType, StateType> {
   render() {
     return (
       <StyledLogin>
-        <LoginPanel numOAuth={+this.state.hasGithub + +this.state.hasGoogle}>
+        <LoginPanel hasBasic={this.state.hasBasic} numOAuth={+this.state.hasGithub + +this.state.hasGoogle}>
           <OverflowWrapper>
             <GradientBg />
           </OverflowWrapper>
@@ -465,8 +465,8 @@ const GradientBg = styled.div`
 
 const LoginPanel = styled.div`
   width: 330px;
-  height: ${(props: { numOAuth: number }) =>
-    430 + props.numOAuth * 50}px;
+  height: ${(props: { numOAuth: number, hasBasic: boolean }) =>
+    280 + +props.hasBasic * 150 + props.numOAuth * 50}px;
   background: white;
   margin-top: -20px;
   border-radius: 10px;

+ 59 - 50
dashboard/src/main/auth/Register.tsx

@@ -20,6 +20,7 @@ type StateType = {
   confirmPasswordError: boolean;
   hasGithub: boolean;
   hasGoogle: boolean;
+  hasBasic: boolean;
 };
 
 export default class Register extends Component<PropsType, StateType> {
@@ -29,6 +30,7 @@ export default class Register extends Component<PropsType, StateType> {
     confirmPassword: "",
     emailError: false,
     confirmPasswordError: false,
+    hasBasic: true,
     hasGithub: true,
     hasGoogle: false,
   };
@@ -45,8 +47,9 @@ export default class Register extends Component<PropsType, StateType> {
       .getCapabilities("", {}, {})
       .then((res) => {
         this.setState({ 
-          hasGithub: res.data?.github,
-          hasGoogle: res.data?.google,
+          hasGithub: res.data?.github_login,
+          hasGoogle: res.data?.google_login,
+          hasBasic: res.data?.basic_login,
         });
       })
       .catch((err) => console.log(err));
@@ -133,7 +136,7 @@ export default class Register extends Component<PropsType, StateType> {
           <OAuthButton onClick={this.githubRedirect}>
             <IconWrapper>
               <Icon src={github} />
-              Log in with GitHub
+              Sign up with GitHub
             </IconWrapper>
           </OAuthButton>
       );
@@ -146,14 +149,14 @@ export default class Register extends Component<PropsType, StateType> {
           <OAuthButton onClick={this.googleRedirect}>
             <IconWrapper>
               <StyledGoogleIcon />
-              Log in with Google
+              Sign up with Google
             </IconWrapper>
           </OAuthButton>
       );
     }
   };
 
-  render() {
+  renderBasicSection = () => {
     let {
       email,
       password,
@@ -162,9 +165,55 @@ export default class Register extends Component<PropsType, StateType> {
       confirmPasswordError,
     } = this.state;
 
+    if (this.state.hasBasic) {
+      return <div><InputWrapper>
+        <Input
+          type="email"
+          placeholder="Email"
+          value={email}
+          onChange={(e: ChangeEvent<HTMLInputElement>) =>
+            this.setState({ email: e.target.value, emailError: false })
+          }
+          valid={!emailError}
+        />
+        {this.renderEmailError()}
+      </InputWrapper>
+      <Input
+        type="password"
+        placeholder="Password"
+        value={password}
+        onChange={(e: ChangeEvent<HTMLInputElement>) =>
+          this.setState({
+            password: e.target.value,
+            confirmPasswordError: false,
+          })
+        }
+        valid={true}
+      />
+      <InputWrapper>
+        <Input
+          type="password"
+          placeholder="Confirm Password"
+          value={confirmPassword}
+          onChange={(e: ChangeEvent<HTMLInputElement>) =>
+            this.setState({
+              confirmPassword: e.target.value,
+              confirmPasswordError: false,
+            })
+          }
+          valid={!confirmPasswordError}
+        />
+        {this.renderConfirmPasswordError()}
+      </InputWrapper>
+      <Button onClick={this.handleRegister}>Continue</Button>
+      </div>
+    }
+  }
+
+  render() {
     return (
       <StyledRegister>
-        <LoginPanel numOAuth={+this.state.hasGithub + +this.state.hasGoogle}>
+        <LoginPanel hasBasic={this.state.hasBasic} numOAuth={+this.state.hasGithub + +this.state.hasGoogle}>
           <OverflowWrapper>
             <GradientBg />
           </OverflowWrapper>
@@ -173,7 +222,7 @@ export default class Register extends Component<PropsType, StateType> {
             <Prompt>Sign up for Porter</Prompt>
             {this.renderGithubSection()}
             {this.renderGoogleSection()}
-            {(this.state.hasGithub || this.state.hasGoogle) ? 
+            {(this.state.hasGithub || this.state.hasGoogle) && this.state.hasBasic ? 
               <OrWrapper>
                 <Line />
                 <Or>or</Or>
@@ -181,47 +230,7 @@ export default class Register extends Component<PropsType, StateType> {
               null
             }
             <DarkMatter />
-            <InputWrapper>
-              <Input
-                type="email"
-                placeholder="Email"
-                value={email}
-                onChange={(e: ChangeEvent<HTMLInputElement>) =>
-                  this.setState({ email: e.target.value, emailError: false })
-                }
-                valid={!emailError}
-              />
-              {this.renderEmailError()}
-            </InputWrapper>
-            <Input
-              type="password"
-              placeholder="Password"
-              value={password}
-              onChange={(e: ChangeEvent<HTMLInputElement>) =>
-                this.setState({
-                  password: e.target.value,
-                  confirmPasswordError: false,
-                })
-              }
-              valid={true}
-            />
-            <InputWrapper>
-              <Input
-                type="password"
-                placeholder="Confirm Password"
-                value={confirmPassword}
-                onChange={(e: ChangeEvent<HTMLInputElement>) =>
-                  this.setState({
-                    confirmPassword: e.target.value,
-                    confirmPasswordError: false,
-                  })
-                }
-                valid={!confirmPasswordError}
-              />
-              {this.renderConfirmPasswordError()}
-            </InputWrapper>
-            <Button onClick={this.handleRegister}>Continue</Button>
-
+            {this.renderBasicSection()}
             <Helper>
               Have an account?
               <Link href="/login">Sign in</Link>
@@ -456,8 +465,8 @@ const GradientBg = styled.div`
 
 const LoginPanel = styled.div`
   width: 330px;
-  height: ${(props: { numOAuth: number }) =>
-    450 + props.numOAuth * 50}px;
+  height: ${(props: { numOAuth: number, hasBasic: boolean }) =>
+    270 + +props.hasBasic * 180 + props.numOAuth * 50}px;
   background: white;
   margin-top: -20px;
   border-radius: 10px;

+ 21 - 5
internal/adapter/gorm.go

@@ -21,7 +21,8 @@ func New(conf *config.DBConf) (*gorm.DB, error) {
 		})
 	}
 
-	dsn := fmt.Sprintf(
+	// connect to default postgres instance first
+	baseDSN := fmt.Sprintf(
 		"user=%s password=%s port=%d host=%s",
 		conf.Username,
 		conf.Password,
@@ -30,12 +31,25 @@ func New(conf *config.DBConf) (*gorm.DB, error) {
 	)
 
 	if conf.ForceSSL {
-		dsn = dsn + " sslmode=require"
+		baseDSN = baseDSN + " sslmode=require"
 	} else {
-		dsn = dsn + " sslmode=disable"
+		baseDSN = baseDSN + " sslmode=disable"
 	}
 
-	res, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
+	postgresDSN := baseDSN + " database=postgres"
+	targetDSN := baseDSN + " database=" + conf.DbName
+
+	defaultDB, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{
+		FullSaveAssociations: true,
+	})
+
+	// attempt to create the database
+	if conf.DbName != "" {
+		defaultDB.Exec(fmt.Sprintf("CREATE DATABASE %s;", conf.DbName))
+	}
+
+	// open the database connection
+	res, err := gorm.Open(postgres.Open(targetDSN), &gorm.Config{
 		FullSaveAssociations: true,
 	})
 
@@ -46,7 +60,9 @@ func New(conf *config.DBConf) (*gorm.DB, error) {
 	if err != nil {
 		for {
 			time.Sleep(timeout)
-			res, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
+			res, err = gorm.Open(postgres.Open(targetDSN), &gorm.Config{
+				FullSaveAssociations: true,
+			})
 
 			if retryCount > 3 {
 				return nil, err