Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add login mechanism for react native
- Loading branch information
1 parent
4e6a6f3
commit 2bf767f
Showing
6 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class SessionsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
skip_before_action :verify_authenticity_token | ||
|
||
respond_to :json | ||
def create | ||
resource = User.find_by_email(params[:login]) | ||
return head :unauthorized unless resource | ||
|
||
if resource.valid_password?(params[:password]) | ||
sign_in("user", resource) | ||
render json: { success: true } | ||
return | ||
end | ||
return head :unauthorized | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,6 @@ | |
root "counters#index" | ||
end | ||
end | ||
|
||
post "sessions/create" => "sessions#create" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { StyleSheet, Text, View, TextInput, Button } from 'react-native'; | ||
import React, { useState } from 'react'; | ||
import { fetchData, setCookie } from './client'; | ||
|
||
export default function Login({ onLogin }) { | ||
const [username, setUsername] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
|
||
const handleLogin = () => { | ||
fetchData('/sessions/create', 'POST', { login: username, password }).then((response) => { | ||
if (response.error) { | ||
console.error(response.error); | ||
} | ||
else if (response.status === 200) { | ||
console.log('Logged in'); | ||
// hold the cookie for future requests | ||
cookie = response.headers.map['set-cookie']; | ||
setCookie(cookie); | ||
onLogin(); | ||
} | ||
else if (response.status === 401) { | ||
console.error('Login failed'); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<TextInput | ||
style={styles.input} | ||
placeholder="Username" | ||
placeholderTextColor={'#aaa'} | ||
value={username} | ||
onChangeText={setUsername} | ||
/> | ||
<TextInput | ||
style={styles.input} | ||
placeholder="Password" | ||
placeholderTextColor={'#aaa'} | ||
secureTextEntry | ||
value={password} | ||
onChangeText={setPassword} | ||
/> | ||
<Button title="Login" onPress={handleLogin} /> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
title: { | ||
fontSize: 48, | ||
fontWeight: 'bold', | ||
padding: 20, | ||
color: '#fff', | ||
}, | ||
container: { | ||
flex: 1, | ||
backgroundColor: '#000', | ||
|
||
alignItems: 'stretch', | ||
justifyContent: 'top', | ||
}, | ||
input: { | ||
height: 40, | ||
margin: 12, | ||
borderWidth: 1, | ||
color: '#fff', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,31 @@ | ||
const baseUrl = 'http://192.168.1.176:3000'; | ||
let cookie = ''; | ||
|
||
export const fetchData = () => { | ||
return fetch('http://192.168.1.176:3000/counters.json') | ||
.then(response => response.json()) | ||
export const fetchData = (endpoint, method, data) => { | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json' | ||
}; | ||
if (cookie) { | ||
headers['Cookie'] = cookie; | ||
} | ||
const options = { | ||
method: method, | ||
headers: headers | ||
}; | ||
if (method === 'POST' || method === 'PUT') { | ||
options.body = JSON.stringify(data); | ||
} | ||
console.log(options) | ||
return fetch(`${baseUrl}${endpoint}`, options) | ||
.then(response => response) | ||
.catch(error => console.error('Error:', error)); | ||
}; | ||
}; | ||
|
||
export const setCookie = (c) => { | ||
cookie = c; | ||
} | ||
|
||
export const getCookie = () => { | ||
return cookie; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters