Rails Api ,React,Docker環境でRailsのgemのDeviseを使用してログイン環境を実装しようとしています。
ログイン環境を実装したつもりなのですが、post が通信されておらずnet::ERR_CONNECTION_REFUSED
とエラーが出てAxiosErrorのような状態になってしまいます。
auth.js
1import client from "./client" 2import Cookies from "js-cookie" 3 4// サインアップ 5export const signUp = (data) => { 6 return client.post("auth", data); 7}; 8 9// サインイン 10export const signIn = (data) => { 11 return client.post("auth/sign_in", data); 12}; 13 14// サインアウト 15export const signOut = () => { 16 return client.delete("auth/sign_out",{ 17 headers: { 18 "access-token": Cookies.get("_access_token"), 19 "client": Cookies.get("_client"), 20 "uid": Cookies.get("_uid"), 21 } 22 }); 23}; 24 25// ログインユーザーの取得 26export const getCurrentUser = () => { 27 if ( 28 !Cookies.get("_access_token") || 29 !Cookies.get("_client") || 30 !Cookies.get("_uid") 31 ) return 32 return client.get("/auth/sessions", { 33 headers: { 34 "access-token": Cookies.get("_access_token"), 35 "client": Cookies.get("_client"), 36 "uid": Cookies.get("_uid"), 37 }, 38 }); 39}; 40
client.js
1import applyCaseMiddleware from "axios-case-converter" 2import axios from "axios" 3 4 5const options = { 6 ignoreHeaders: true 7} 8 9const client = applyCaseMiddleware( 10 axios.create({ 11 baseURL:"http://localhost:3001/api/v1" 12}), options) 13 14export default client
signup.jsx
1import React, { useContext, useState } from "react" 2import { useNavigate, Link } from "react-router-dom" 3import Cookies from "js-cookie" 4 5import { makeStyles, Theme } from "@material-ui/core/styles" 6import TextField from "@material-ui/core/TextField" 7import Card from "@material-ui/core/Card" 8import CardContent from "@material-ui/core/CardContent" 9import CardHeader from "@material-ui/core/CardHeader" 10import Button from "@material-ui/core/Button" 11 12import { AuthContext } from "../../App" 13import AlertMessage from "../utils/AlertMessage" 14import { signUp } from "../../lib/api/auth" 15 16 17 18const useStyles = makeStyles((theme: Theme) => ({ 19 csubmitBtn: { 20 paddingTop: theme.spacing(2), 21 textAlign: "right", 22 flexGrow: 1, 23 textTransform: "none" 24 }, 25 header: { 26 textAlign: "center" 27 }, 28 card: { 29 padding: theme.spacing(2), 30 maxWidth: 400 31 } 32})) 33 34export const SignUp = () => { 35 const classes = useStyles() 36 const history = useNavigate() 37 38 const { setIsSignedIn, setCurrentUser } = useContext(AuthContext); 39 const [name, setName] = useState("") 40 const [email, setEmail] = useState("") 41 const [password, setPassword] = useState(""); 42 const [passwordConfirmation, setPasswordConfirmation] = useState(""); 43 const [alertMessageOpen, setAlertMessageOpen] = useState(false) 44 45 46 const handleSubmit = async (e) => { 47 48 e.preventDefault() 49 50 const data = { 51 name: name, 52 email: email, 53 password: password, 54 passwordConfirmation: passwordConfirmation 55 } 56 57 try { 58 const res = await signUp(data) 59 console.log(res) 60 61 if (res.status === 200) { 62 Cookies.set("_access_token", res.headers["access-token"]) 63 Cookies.set("_client", res.headers["client"]) 64 Cookies.set("_uid", res.headers["uid"]) 65 66 setIsSignedIn(true) 67 setCurrentUser(res.data.data) 68 69 history.push("/") 70 71 console.log("Signed in successfully!") 72 } else { 73 setAlertMessageOpen(true) 74 } 75 } catch (err) { 76 console.log(err) 77 setAlertMessageOpen(true) 78 } 79 } 80 81 return ( 82 <> 83 <h1>サインアップページです</h1> 84 <form noValidate autoComplete="off"> 85 <Card className={classes.card}> 86 <CardHeader className={classes.header} title="Sign Up" /> 87 <CardContent> 88 <TextField 89 variant="outlined" 90 required 91 fullWidth 92 label="Name" 93 value={name} 94 margin="dense" 95 onChange={event => setName(event.target.value)} 96 /> 97 <TextField 98 variant="outlined" 99 required 100 fullWidth 101 label="Email" 102 value={email} 103 margin="dense" 104 onChange={event => setEmail(event.target.value)} 105 /> 106 <TextField 107 variant="outlined" 108 required 109 fullWidth 110 label="Password" 111 type="password" 112 value={password} 113 margin="dense" 114 autoComplete="current-password" 115 onChange={event => setPassword(event.target.value)} 116 /> 117 <TextField 118 variant="outlined" 119 required 120 fullWidth 121 label="Password Confirmation" 122 type="password" 123 value={passwordConfirmation} 124 margin="dense" 125 autoComplete="current-password" 126 onChange={event => setPasswordConfirmation(event.target.value)} 127 /> 128 <div style={{ textAlign: "right"}} > 129 <Button 130 type="submit" 131 variant="contained" 132 size="large" 133 fullWidth 134 color="default" 135 disabled={!name || !email || !password || !passwordConfirmation ? true : false} 136 className={classes.submitBtn} 137 onClick={handleSubmit} 138 > 139 Submit 140 </Button> 141 </div> 142 </CardContent> 143 </Card> 144 </form> 145 <AlertMessage // エラーが発生した場合はアラートを表示 146 open={alertMessageOpen} 147 setOpen={setAlertMessageOpen} 148 severity="error" 149 message="Invalid email or password" 150 /> 151 <Link to="/signin">サインインへ</Link> 152 </> 153 ) 154} 155
あなたの回答
tips
プレビュー