feat: seed data only in dev mode (#61)

This commit is contained in:
STEVEN 2022-05-19 22:29:27 +08:00 committed by GitHub
parent 304df8674a
commit d6680a0bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 40 deletions

View File

@ -1,17 +1,19 @@
INSERT INTO INSERT INTO
user ( user (
`id`, `id`,
`name`,
`email`, `email`,
`role`,
`name`,
`open_id`, `open_id`,
`password_hash` `password_hash`
) )
VALUES VALUES
( (
101, 101,
'guest', 'steven@memos.com',
'guest@example.com', 'OWNER',
'guest_open_id', 'Steven',
'steven_open_id',
-- raw password: secret -- raw password: secret
'$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK' '$2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK'
); );

View File

@ -54,8 +54,11 @@ func (db *DB) Open() (err error) {
if err := db.migrate(); err != nil { if err := db.migrate(); err != nil {
return fmt.Errorf("failed to migrate: %w", err) return fmt.Errorf("failed to migrate: %w", err)
} }
if err := db.seed(); err != nil { // If mode is dev, then seed the database.
return fmt.Errorf("failed to seed: %w", err) if db.mode == "dev" {
if err := db.seed(); err != nil {
return fmt.Errorf("failed to seed: %w", err)
}
} }
} else { } else {
// If db file exists and mode is dev, we should migrate and seed the database. // If db file exists and mode is dev, we should migrate and seed the database.

View File

@ -68,14 +68,6 @@
@apply cursor-wait opacity-80; @apply cursor-wait opacity-80;
} }
} }
> .btn-text {
@apply text-sm;
}
> .split-text {
@apply text-gray-400 mx-2;
}
} }
> .tip-text { > .tip-text {

View File

@ -25,6 +25,10 @@ const Signin: React.FC<Props> = () => {
useEffect(() => { useEffect(() => {
api.getSystemStatus().then((status) => { api.getSystemStatus().then((status) => {
setSiteOwner(status.owner); setSiteOwner(status.owner);
if (status.profile.mode === "dev") {
setEmail("steven@memos.com");
setPassword("secret");
}
pageLoadingState.setFinish(); pageLoadingState.setFinish();
}); });
}, []); }, []);
@ -105,28 +109,6 @@ const Signin: React.FC<Props> = () => {
actionBtnLoadingState.setFinish(); actionBtnLoadingState.setFinish();
}; };
const handleAutoSigninAsGuestBtnClick = async () => {
if (actionBtnLoadingState.isLoading) {
return;
}
try {
actionBtnLoadingState.setLoading();
await api.login("guest@example.com", "secret");
const user = await userService.doSignIn();
if (user) {
locationService.replaceHistory("/");
} else {
toastHelper.error("😟 Login failed");
}
} catch (error: any) {
console.error(error);
toastHelper.error("😟 " + error.message);
}
actionBtnLoadingState.setFinish();
};
return ( return (
<div className={`page-wrapper signin ${pageLoadingState.isLoading ? "hidden" : ""}`}> <div className={`page-wrapper signin ${pageLoadingState.isLoading ? "hidden" : ""}`}>
<div className="page-container"> <div className="page-container">
@ -149,10 +131,6 @@ const Signin: React.FC<Props> = () => {
</div> </div>
</div> </div>
<div className="action-btns-container"> <div className="action-btns-container">
<button className={`btn ${actionBtnLoadingState.isLoading ? "requesting" : ""}`} onClick={handleAutoSigninAsGuestBtnClick}>
Login as Guest
</button>
<span className="split-text">/</span>
{siteOwner ? ( {siteOwner ? (
<button <button
className={`btn signin-btn ${actionBtnLoadingState.isLoading ? "requesting" : ""}`} className={`btn signin-btn ${actionBtnLoadingState.isLoading ? "requesting" : ""}`}